I have been fighting the timezone battle for weeks now, everyone thinks they know the solution and then they get a million upvotes but the reality is I am totaly 100% stuck, no answer ever given for timezone support in PHP/MySQL has worked 100% for me, so I am asking the question again on here in as much detail as I possibly can with code sample and all.
If you are a PHP/MySQL expert, or even a begginner with crazy good luck and have gotten php/mysql/ user submitted timezone working, then please do help.
Below is what I have so far, please read the comments to see what kind of issues I am having. Basicly I can show a date and time in the users timezone if it is in the form of a timestamp like this "1262819848" but the NEWER versions of MySQL return a TIMESTAMP like this instead "2010-01-06 23:17:28".
If I could pass "2010-01-06 23:17:28" into this date("m:d:y h:i:s A",$time_stamp) then things would work GREAT but instead it needs a timestamp paased in. The only way I can do this is to eather store a real timestamp in an INTEGER field in MySQL OR else get the value "2010-01-06 23:17:28" and then convert it to a timestamp which just seems like to much overhead when I can store it already converted in an INT field.
If you can help me use a datetime functions then I would greatly appreciate the help, I have been stuck on this problem for a couple YEARS now. I am about to give up and use the Integer field like everyone on SO says is bad for some reason.
<?PHP
/////////////////////////////////////////////////////////
// Get a MySQL time / date and show in user timezone //
////////////////////////////////////////////////////////
//set the user's time zone for this page
//this assumes the user has already chosen a timezone and we are getting it from a PHP session value now
// example value is "America/New_York" which is -5
date_default_timezone_set($_SESSION['time_zone']);
// get a MySQL result with a time/date value in it
$sql = 'SELECT user_id,date from online_users WHERE user_id = 1';
$result = executeQuery($sql);
// set our date/time value from MySQL into a variable
if ($line_online = mysql_fetch_assoc($result)){
$time_stamp = $line_online['date'];
}
// format our date/time value TIMESTAMP any way we like now!
// MySQL result MUST be in this format '1262291376'
$date_format = date("m:d:y h:i:s A",$time_stamp); // formats timestamp in mm:dd:yy
echo $date_format;
/////////////////////////////////////////////////////////
// Add a time / date to MySQL //
////////////////////////////////////////////////////////
// this add a time and date to MySQL and looks like this in the MySQL table "2010-01-06 23:17:28"
// that is a TIMESTAMP field in MySQL, newer version of mysql store a timestamp like this "2010-01-06 23:17:28" instead of like this "1262819848"
$sql = "INSERT INTO users (`id`, `datetime`) VALUES ('', UTC_TIMESTAMP())";
executeQuery($sql);
// So if I query this record and try to show it like I do in the code above in the "Get mysql result section" It will not work correctly because
// because it is like this "2010-01-06 23:17:28" instead of like this "1262819848"
// The ONLY solution I have found is to store the UTC timestamp into an INTEGER field instead of a DATETIME or TIMESTAMP field.
// Everyone on this site says this is wrong and I could use the default date and time stuff but I have yet to get it working.
// If I could store a timestamp in mysql like a timestamp with no "-" dashes in it, then it would work correctly when I show it on page.
// Basicly I have this half done, I can show the time and date in a users timezone as long as I pass the PHP a real TIMESTAMP like this "1262819848"
?>
Based on past responses to a similar question, plus reading every post on this site related to php/mysql and timezones, I will probably not resolve this issue so I will start a bounty on this as soon as it let's me. Thanks