Convert date string to UTC time with PHP - php

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)

Related

Sugarcrm date not showing properly

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

Strange bug of date_modify function in DateTime class

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?

PHP MongoDate toDateTime default timezone

When I create new DateTime object, it has timezone from "date.timezone" setting:
print_r(new DateTime());
DateTime Object
(
[date] => 2015-03-02 03:19:50.000000
[timezone_type] => 3
[timezone] => Europe/Moscow
)
But when I get DateTime object from MongoDate, it has UTC timezone:
print_r((new MongoDate()) -> toDateTime())
DateTime Object
(
[date] => 2015-03-02 00:19:50.000000
[timezone_type] => 1
[timezone] => +00:00
)
How can I setup MongoDate to create DateTime objects with my default timezone?
A way from do this:
$mongoDate = new \MongoDate();
$dateTimeDefaultTimeZone = $mongoDate->toDateTime()->setTimezone(new \DateTimeZone(date_default_timezone_get()))
You will can get the DateTime with default TimeZone.

Get month from mssql DateTime Object with php

When I print the Array I get this
[CreatedDate] => DateTime Object ( [date] => 2013-03-20 00:00:00 [timezone_type] => 3 [timezone] => America/Denver )
I'm trying to pull the month from the date, so far no luck. Been doing a combination of the below code and got a date to return but it was 12/31/1969 which is not in my database
$month = date("m",($row['CreatedDate']));
$month = date("m",($row['CreatedDate.date']));
$month = date("m",($row['date']));
Whatever produced the DateTime object already has what you need. You just need to call format() to get the month:
echo $object->format('m');

Changing timezone of a retrieved date in php

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.

Categories