javascript - Send JSON date to a PHP backend, timezone is lost -
i missing dates here.
assume have basic input page, this
markup
<input type="date" ng-model="item.date"> <button ng-click="test(item)">test</button>
angular module
angular.module('test', []).controller('ctrl', function($scope, $http){ $scope.test = function(item) { $http.post('___', item); } });
then on server side there's trivial code.
date_default_timezone_set ( 'europe/rome' ); var_dump( new \datetime($input['date']) );
being $input['date']
post-ed json date value, holds iso format following:
2016-03-22t23:00:00.000z
now, having timezone set, expect above mentioned iso date handled correct timezone, see instead this
object(datetime)#1 (3) { ["date"]=> string(26) "2016-03-22 23:00:00.000000" ["timezone_type"]=> int(2) ["timezone"]=> string(1) "z" }
which should resolved instead 2016-03-23 utc+1.
tl;dr
shouldn't following code
date_default_timezone_set ( 'europe/rome' ); var_dump( new \datetime('2016-03-22t23:00:00.000z');
resolve 2016-03-23 utc+1 ?
your code working correctly, not intended.
your javascript date generated in browser, - assume - timezone set europe/rome
already. if use datepicker plugin, cited date if pick 2016-03-24
, converted browser local timezone. variable stored in not carry information timezone, z
part of date string assumes it's utc.
on server you're initializing date date string, , z
used timezone. far know, no valid timezone code, utc assumed.
you have explicitly state in datetime constructor timezone use date,
$date = new datetime($input['date'], new datetimezone('europe/rome'));
you're gonna have remove 'z' part of date.
i have put illustrate problem: http://sandbox.onlinephpfunctions.com/code/a545a7a87e3ad0f2e6eae1d134147aff30aec29f
<?php //date_default_timezone_set ( 'europe/rome' ); $date = new \datetime("2016-03-23t23:00:00.000", new datetimezone('utc') ); var_dump( $date ); // 2016-03-23 23:00:00.000000 $timezone = new datetimezone('europe/rome'); $date->settimezone($timezone); var_dump( $date ); // 2016-03-24 00:00:00.000000
edit:
try following code in html:
<input type="date" ng-model="item.date" ng-model-options="{timezone: 'utc'}">
your date in form 2016-03-18t00:00:00.000z
, can send server is.
on server, if parse this, so
$date = new \datetime("2016-03-18t00:00:00.000z" ); var_dump( $date );
this output
object(datetime)#1 (3) { ["date"]=> string(26) "2016-03-18 00:00:00.000000" ["timezone_type"]=> int(2) ["timezone"]=> string(1) "z" }
which closer wanted do.
Comments
Post a Comment