How to Convert PHP Date Formats to Gmt and Vice Versa

how to convert php date formats to GMT and vice versa?

Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.

Here's code to switch to GMT

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));

and back to the default timezone...

$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.

e.g.

// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');

// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');

// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));

// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');

// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');

There's a whole lot of functions you can use, that are much more readable that the procedural functions.


Explicit example of converting from a timezone to GMT

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');

Using your Asia/Kolkata to America/New_York

date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00

Using PHP DateTime to parse and convert timezone PST to PDT (GMT -8 to GMT -7)

Not the greatest but it's the only way I can think of to do this

$tz = new DateTimeZone('America/Los_Angeles');  
$saleEndDate = new DateTime("2016-11-07T17:30:00-08:00");
$saleEndDate->setTimezone($tz);
$stamp = $saleEndDate->format('U');
$zone = $tz->getTransitions($stamp, $stamp);
if(!$zone[0]['isdst']) $saleEndDate->modify('+1 hour');
echo $saleEndDate->format('Y-m-d H:i:s');

What I'm doing here is using the DateTimeZone::getTransitions function to determine if the date you provided is DST or not. If it isn't, we add one hour. Note that this does not change the time zone, it just corrects for the DST shift

You can see it in action here

Change Sydney Date Time to GMT in PHP

Perhaps your web-server is not set up in the Australia/Sydney timezone ?
The following answer will help you: how to convert php date formats to GMT and vice versa?

Here is what you can do:

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');

Converting Unix time to GMT datetime with php

You can change the time to different timezone. The PHP function is:

date_default_timezone_set()

More info: http://php.net/manual/en/function.date-default-timezone-set.php

PHP 5.2.17: convert local time to GMT and GMT to local time

Perhaps if you try with DateTime object you would get better result :

<?php
function GmtTimeToLocalTime($time) {
$date = new DateTime(date('Y-m-d h:i:s',$time),new DateTimezone('UTC'));
$date->setTimezone(new \DateTimezone('Asia/Calcutta'));
return $date->format("Y-m-d H:i:s");
}

php new dateTime and gmdate

In that case you need two object one for GMT.

$oDate = new DateTime($date);
$gmDate = new DateTime($date, new DateTimeZone('GMT'));

array(
'post_date' => $oDate->format('Y-m-d H:i:s'),
'post_date_gmt' => $gmDate->format('Y-m-d H:i:s'),
);

How can I easily convert dates from UTC via PHP?

Here's what we did with our servers. We set everything to use UTC, and we display in the user's time zone by converting from UTC on the fly. The code at the bottom of this post is an example of how to get this to work; you should confirm that it works in all cases with your setup (i.e. daylight savings, etc).

Configuring CentOS

  1. Edit /etc/sysconfig/clock and set ZONE to UTC
  2. ln -sf /usr/share/zoneinfo/UTC /etc/localtime

Configuring MySQL

  1. Import timezones into MySQL if necessary:

    mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

  2. Edit my.cnf and add the following within the [mysqld] section:

    default-time-zone = 'UTC'

PHP Code

<?php
/*
Example usage:
$unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13');
echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime);
*/

// You should move this to your regular init method
date_default_timezone_set('UTC'); // make this match the server timezone

class TimeUtil {
public static function timestampToDateTime($timestamp) {
return gmdate('Y-m-d H:i:s', $timestamp);
}

public static function dateTimeToTimestamp($dateTime) {
// dateTimeToTimestamp expects MySQL format
// If it gets a fully numeric value, we'll assume it's a timestamp
// You can comment out this if block if you don't want this behavior
if(is_numeric($dateTime)) {
// You should probably log an error here
return $dateTime;
}
$date = new DateTime($dateTime);
$ret = $date->format('U');
return ($ret < 0 ? 0 : $ret);
}

public static function UTCToPST($format, $time) {
$dst = intval(date("I", $time));
$tzOffset = intval(date('Z', time()));
return date($format, $time + $tzOffset - 28800 + $dst * 3600);
}

}


Related Topics



Leave a reply



Submit