According to this calculator this date should be Mon Apr 01 2013 13:11:57 GMT+0200. But instead it's the servers time: 2013-04-01 07:11:57. I get that this is a timezone problem (the server is in -4) but is there a way to make this timezone independent or do I have to query the user's timezone from the database?
$date = 1364814717;
$date = date('Y-m-d H:i:s', $date);
You can change the default time zone like this:
date_default_timezone_set('UTC');
If you need to make this different per-user, you could always send the unconverted number and use javascript (see getTimezoneOffset), but it's probably better to let the user choose (their computer might not know where it really is!)
I hope this is what you are looking for.
var myDate = new Date();
document.write(myDate.getTimezoneOffset());
You will need to ask where the user is and store it in it's profile in the database. You can't rely on anything actually. As users can be behind a proxy and seem to be like in Shang Hai, but are actually in Melbourne.
For example the user says it is in GMT +1
. You put the option list in the user's profile and when the user saves it, it will be written to a database row called (for example) timezone
.
Try to use the newer DateTime
style (example from user with ID 7);
PHP
$mysqli = new mysqli("localhost", "my_user", "my_password", "database_name");
if ($result = $mysqli->query("SELECT timezone FROM users where user_id = 7")) {
$prefix = '';
if ($result[0]->timezone >= 0) {
$prefix = '+';
}
$date = new DateTime('NOW GMT'.$prefix.$result[0]->timezone);
echo '<pre>';
print_r($date);
echo '</pre>';
}
else {
echo 'Query failed';
}
What happens here is you only save the GMT offset (or use whatever timeset you want) and return the current time based on the user's preference.
Output
DateTime Object (
[date] => 2013-04-01 13:50:08
[timezone_type] => 1
[timezone] => +01:00
)
Don't forget to look into summertime ;)
Good luck!