I am writing a PHP function that calculates the difference the current time and a timestamp (eg: 2011-10-21 11:16:00) which is retrieved from a mySQL database table.
Problem:
The calculated time difference seems to be off by 6 hours! I checked through the code and I cant figure out why a timestamp made a few minutes ago result in a time difference of 6 hours! However when I set the variable $datetime_now to a specific value, the calculated time difference is correct! Any help to point me in the right direction is greatly appreciated!
PHP Code:
$datetime_now = new DateTime;
$datetime_post = new DateTime($results[$key]['post_timestamp']); //ie. 2011-10-21 11:16:00
$interval = $datetime_now->diff($datetime_post);
$hours = $interval->format('%h');
Additional Info:
I am running the web server (XAMPP package) on a local machine, and accessing the webpage from the same system.
Timezones, I’m guessing you’re GMT-6? Correct this by using UTC for all of your transactions, then at the last stage making a conversion to local time. Check your server locale settings and I bet you they will be different from where you’re living.