SCENARIO
So I've custom object where I've three fields called start_date, end_date and booking_expire_time(for an event).
My dates are coming from Magento in form of String ofc, I already have logged so they are coming like
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $start_date - 08/30/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $event_endt_time - 08/31/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $booking_expire_time - 08/30/2018 12:59 PM
Which is correct and same which I've chosen in Magento
And I'm storing it like following in that bean.
$st = date_create_from_format("m/d/Y H:i A",$start_date);
$eventBean->start_date = date_format($st, 'Y-m-d H:i:s');
$en = date_create_from_format("m/d/Y H:i A",$event_end_time);
$eventBean->event_end_time = date_format($en, 'Y-m-d H:i:s');
$ex = date_create_from_format("m/d/Y H:i A",$booking_expire_time);
$eventBean->booking_expire_time = date_format($ex, 'Y-m-d H:i:s');
ISSUE
There is no problem for saving except when I go to the UI and check date and time, the date and time are different.
I tried changing user timezone and storing it again but it is still same, I tried setting the timezone to UTC in User settings but still, the same thing is happening.
I've logged the date time which getting retrieved in code and it prints following for start date and end date
DateTime Object
(
[date] => 2018-08-30 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
DateTime Object
(
[date] => 2018-08-31 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
EDIT
Timezone setting
PHPInfo from Diagnostic Tool
Related
I'm having trouble understanding how my code adapts to DST, as it's incorrect with the recent update. I'm storing a date time in the database based on UTC and then converting it back to the local timezone for display. If PHP is taking DST into account, something else is wrong because all of my stored dates are 1 hour off.
$stored_date = '2016-11-16 12:04:01'; // in UTC
$dateTime = new DateTime($stored_date, new DateTimeZone('UTC'));
$dateTimeZone = new DateTimeZone('America/New_York');
$dateTime->setTimezone($dateTimeZone);
print_r($dateTime);
Last week, before DST ended, this would have printed out 2016-11-16 08:04:01. This week, now that DST has ended, it prints out 2016-11-16 07:04:01. Why the hour difference if PHP is properly handing the DST shift?
It shouldn't matter the server settings (I don't think) because I'm explicitly doing the conversion within PHP, right?
I'm ready to start doing a check with PHP to see if DST is in effect and offsetting the conversion by 1 hour because I can't figure out why that hour isn't being automatically compensated for within the DateTime class.
New York city switches between these time zones:
Winter: EST (Eastern Standard Time) = UTC -5
Summer: EDT (Eastern Daylight Time) = UTC -4
According to timeanddate.com the switch will happen on 6th of November. Thus the result is correct: 12 - 5 = 7
In other words, PHP is perfectly aware of DST, as we can see in the following code:
$dateTime = new DateTime('2016-11-05 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
$dateTime = new DateTime('2016-11-06 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
Sat, 05 Nov 2016 08:04:01 -0400
Sun, 06 Nov 2016 07:04:01 -0500
You can inspect the exact information available in your system's time database:
$timeZone = new DateTimeZone('America/New_York');
print_r($timeZone->getTransitions(mktime(0, 0, 0, 1, 1, 2016), mktime(0, 0, 0, 12, 31, 2016)));
Array
(
[0] => Array
(
[ts] => 1451602800
[time] => 2015-12-31T23:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
[1] => Array
(
[ts] => 1457852400
[time] => 2016-03-13T07:00:00+0000
[offset] => -14400
[isdst] => 1
[abbr] => EDT
)
[2] => Array
(
[ts] => 1478412000
[time] => 2016-11-06T06:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
)
I use the native PHP DateTime class for adding days to dates. But when dealing with negative dates, I encountered a strange bug. Depending on the millennium added or a day or two. Example:
$date_one = date_create("-1000-12-27");
date_modify($date_one, '+1 day');
//Return DateTime Object ( [date] => -1000-12-29 00:00:00 )
$date_two = date_create("-2000-12-27");
date_modify($date_two, '+1 day');
//Return DateTime Object ( [date] => -2000-12-28 00:00:00 )
$date_three = date_create("-3000-12-27");
date_modify($date_three, '+1 day');
//Return DateTime Object ( [date] => -3000-12-29 00:00:00 )
That is, depending on the parity of the millennium issue, or December 28 or December 29. Why is this happening? What is the problem?
im passing a startDate end an endDate as a get request parameter to a method,
here they get parsed like :
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$logger->info('startdate is :'.$startDate.', endDate is : '.$endDate.'');
$start=new \DateTime($startDate);
$end=new \DateTime($endDate);
when i log those two parameters, they may be
startdate is: Wed Jan 12 2011 00:00:00 GMT 0100 (CET)
startDate is: Sat Jan 12 2013 00:00:00 GMT 0100 (CET)
so far so good, but if i log the DateTimeĀ“s instanciated from the string above it returns
DateTime Object ( [date] => 0100-01-12 00:00:00 [timezone_type] => 2 [timezone] => GMT )
DateTime Object ( [date] => 0100-01-15 00:00:00 [timezone_type] => 2 [timezone] => GMT )
you can see, the DateTime does not represent the same Date
can i make a valid DateTime from those Strings ?
Update :
i tryed to use createFromFormat
like
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$start=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate);
$end=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate);
but that causes exception :
FatalErrorException: Parse: syntax error, unexpected 'createFromFormat' (T_STRING), expecting variable (T_VARIABLE) or '$' in
i also tryed :
$start=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate));
$end=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate));
But that creates Dates a new Date from right now ( 2014-01-21 12:28:57 )
I just dont get it right.
for any help, thanks in advance!
Your input datetime string Wed Jan 12 2011 00:00:00 GMT 0100 (CET) is not valid/standard for use in DateTime() or strtotime(). See date_parse() function to see how your datetime string is being parsed:
print_r( date_parse('Wed Jan 12 2011 00:00:00 GMT 0100 (CET)') );
demo
Use DateTime::createFromFormat() static method to return DateTime object according to the specific format.
demo
The date format you are probably using is RFC2822. As shown on the PHP date() page as this:
Thu, 21 Dec 2000 16:01:07 +0200
You switched the month and day parts and PHP was unable to determine the correct parts.
Best practice would be to either use a Unix-Timestamp (seconds after Epoch) or a better format like ISO 8601 (2004-02-12T15:19:21+00:00).
I am retrieving a date in format of 2013-09-15 08:45:00 from the database, which is set in UTC and I need to change it to another dynamic timezone (based on user)
So far I've got
$datetime = $row->field_data_field_performance_times_field_performance_times_v;
$eventDate = DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone($user->timezone));
$performance_time = date_format($eventDate, 'l, j F, Y, H:i');
But it doesn't change the timezone. Any ideas what's wrong? It should be +2 hours in my case.
Your input datetime is in UTC, not user's timezone. So first you must create datetime object in UTC, and then set/change timezone to user's :
$dt = new DateTime('2013-09-15 08:45:00', new DateTimeZone('UTC'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 08:45:00
[timezone_type] => 3
[timezone] => UTC
)
*/
Now you have datetime in UTC timezone. If you wish to change timezone, just call ->setTimezone() on DateTime object :
$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 10:45:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
*/
p.s. because input 2013-09-15 08:45:00 is in standard datetime format, you don't need to use DateTime::createFromFormat.
I have the following date string
$date="Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)"
I want to convert it to UTC time
$timestamp_UNIX = strtotime($date);
echo date("Y-m-d\TH:i:s\Z",$timestamp_UNIX);
Why do I got
2011-04-30T11:47:47Z
and not
2011-04-30T09:47:47Z
The problem is that you code does not automatically echo UTC. It echos the timestamp in whatever your default timezone is set to. This is done via date_default_timezone_set() at runtime or via the configuration setting date.timezone in your php.ini.
The modern way would be to use the DateTime and the DateTimeZone classes.
$d = new DateTime('Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)');
print_r($d);
$d->setTimezone(new DateTimeZone('UTC'));
print_r($d);
prints
DateTime Object
(
[date] => 2011-04-30 18:47:47
[timezone_type] => 1
[timezone] => +09:00
)
DateTime Object
(
[date] => 2011-04-30 09:47:47
[timezone_type] => 3
[timezone] => UTC
)
You should use gmdate() instead of date() (or you could check the DateTime and DateTimeZone classes in PHP 5.2 / 5.3)