I read a mysql timestamp can only hold a value from 19700101000000 to sometime in the year 2037. I seriously doubt my app will be around then, well i’m sure it wont but any idea what will people use then for a timestamp, a text field?
Below is an example of how I currently insert a mysql record with a datetime mysql field and you can see I use now() in the PHP below, now I am re-coding my site and I am going to offer timezone support for showing correct times to users so I needd to save date and time as a UTC timestamp.
<?PHP
$sql = "INSERT INTO online_users
(user_id,
online_id,
ip,
datetime,
location,
gender)
values ('$cache->userid,$unid,$LOCALIP,NOW(),$location,$g)";
executeQuery($sql);
?>
Based on my code above and my information above, how would I format this code to insert the time and date into UTC and should I use a timestamp field or a text field in MySQL? I will need to modify the time in my php when showing it often, for example on some posts I calculate the time that has passed since the mysql time (2 days and 4 hours and 34 seconds ago). I also need to use time to query for things like users in the past week to sign on. I am not asking how to do that stuff I just posted it to show what I will be using time for just in case that would help determine the best method I should be saving it as.
I tried everything mention in this question/answer but nothing worked correctly, in the end the only way I was able to get php timezones date_default_timezone_set() function to work correctly with my date and times from mysql was to store them as an integer and then it works perfect. There is no problem of slower sorting for me because I sort by ID number instead of date but to sort by a date I don’t think would be too difficult once I figure out how many seconds are in between.
When I use an integer field in mysql, I can store a UTC timestamp as this 1262658989. When you use a timestamp or datetime field in mysql, it automatically convert it to this style 2003-04-14 00:00:00 which then makes it much hard to work with timezones for some reason.
Using an integer instead I can get the UTC timestamp in PHP using this code: