Silviu-Marian
Silviu-Marian

Reputation: 10907

Discrepancy between PHP and JS's timestamps

This is insane. How should I deal with it?

In Chrome console:

new Date(2013,0,1).getTime() // 1st of Jan 2013
> 1356991200000
------------
new Date(2013,0,1).getTime()== 1356991200000
> true

Now take that value in PHP:

<?php 
    die(date('l, j F Y'), 1356991200000 / 1000); // cut some ms
?>

I get Monday, 31 December 2012

Is this related to GMT? How do I fix this?

Upvotes: 3

Views: 217

Answers (2)

David M&#252;ller
David M&#252;ller

Reputation: 5351

Javascript works with the timezone on the client whereas PHP works with the servers timezone.

JS: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

var x = new Date()
var currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60

PHP: http://php.net/manual/en/function.date-default-timezone-get.php

<?php
echo date_default_timezone_get();

Upvotes: 6

Gabriel Sosa
Gabriel Sosa

Reputation: 7956

Use either one (I would recommend server side). You can't relay that both will be in sync. One depends of your server and the other depends of the user's computer.

If you need to show something use relative time and update the client side time once the page refresh...

Upvotes: 3

Related Questions