Correctly format time between client (javascript) and server (PHP)

I have a date object in javascript that looks like this:

var date = 2014-12-01T00:00:00.000Z

      

And if I do this:

var date = new Date(date);
date.setUTCMonth(date.getUTCMonth()+1);

      

The method then toISOString()

shows the expected value:

2015-01-01T00: 00: 00.000Z

But when I post a date time string via POST to a PHP script and parse it for a date using a class DateTime

, I get this:

2014-12-31 19:00:00 -0500

I tried to set the timezone using a class DateTimeZone

and set the timezone for my own and UTC with no luck:

new DateTime($_POST['DateTime'],new DateTimeZone('America/Mexico_City'));
new DateTime($_POST['DateTime'],new DateTimeZone('UTC'));

      

Is there a way to set the timezone in javascript using a class Date

? Is there any other way to get around this? I have little if not zero experience with timezones.

Update

Here's my ISO formatted time string as per the toISOString()

(javascript) method :

2014-09-01T00: 00: 00.000Z

And here is the content of my $_POST

var according to print_r()

:

Array (
    [DateTime] => 2014-09-01T00:00:00.000Z
)

      

Here's the output of my formatting function using the '%c'

(es_MX locale) format :

dom 31 ago 2014 19:00:00 CDT

And the mentioned formatting function:

function formatDate($date) {
    return utf8_encode(strftime('%c', $date->getTimestamp()));
}

      

+3


source to share


2 answers


I think when you send a date string it is somehow in a different format. At least when I tested this it works as expected:

$ a = new DateTime ('2015-01-01T00: 00: 00.000Z');
echo $ a-> format ('Ymd H: i: s'); // Outputs '2015-01-01 00:00:00'


Demo is here .

+1


source


The problem you are getting is that when using the following format

$date = '2015-01-01T00:00:00.000Z'; 

      

You are specifying the time zone.

Note the " Z " at the end of your date, using its equivalent " navigation time zone (GMT) "

Therefore, when you try to run this code, you will get the same date that is not what you expected, even though you specified it DateTimeZone

as a parameter to the constructor.

$dateTime = new DateTime($date, new DateTimeZone('America/Mexico_City')); 
echo $dateTime->format('c'). "\n"; // 2015-01-01T00:00:00+00:00

$dateTime = new DateTime($date, new DateTimeZone('UTC')); 
echo $dateTime->format('c'). "\n"; //2015-01-01T00:00:00+00:00

      

This is the expected PHP behavior and is documented in the DateTime::__construct

documentation in the parameter $timezone

:



Note:

The $ timezone parameter and the current time zone are ignored if $ time is either a UNIX timestamp (for example, @ 946684800) or specifies a time zone (for example, 2010-01-28T15: 00: 00 + 02: 00).

In your case, you are specifying the time zone directly in your date ("Z") and therefore the DateTimeZone object is ignored.

To avoid this, set a custom timezone, either set it in the date time string, or get rid of the Z:

$date = '2015-01-01T00:00:00.000';
$dateTime = new DateTime($date, new DateTimeZone('America/Mexico_City'));
echo $dateTime->format('c'). "\n"; // 2015-01-01T00:00:00-06:00

$dateTime = new DateTime($date, new DateTimeZone('UTC')); 
echo $dateTime->format('c'). "\n"; // 2015-01-01T00:00:00+00:00

$date = '2015-01-01T00:00:00.000 America/Mexico_City';
$dateTime = new DateTime($date);
echo $dateTime->format('c'). "\n"; //2015-01-01T00:00:00-06:00

      

See the code that works for different PHP versions here: Demo

Also consider this related question I recently asked: PHP DateTime Timezones - Constructor vs Setter Method

+1


source







All Articles