PHP DateTime object - Timestamp and Timezones conflict - php

From a DateTime object, I am interested in getting the time in different TimeZones.
As explained in the DateTime::setTimezone doc, this works pretty well when the DateTime object is created from a string:
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:sP') . "\n";
echo $date->getTimestamp() . "\n";
The above examples will output:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
1999-12-31 12:00:00+00:00
946641600
Now is the interesting part: If we pick up our timestamp, and initiate our DateTime Object with it following the manual instructions.
$date2 = new DateTime('#946641600');
$date2->setTimezone(new DateTimeZone('Pacific/Nauru'));
echo $date2->format('Y-m-d H:i:sP') . "\n";
$date2->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date2->format('Y-m-d H:i:sP') . "\n";
$date2->setTimezone(new DateTimeZone('UTC'));
echo $date2->format('Y-m-d H:i:sP') . "\n";
echo $date2->getTimestamp() . "\n";
And here we get: // [edit] humm... Sorry, this output is wrong...
1999-12-31 12:00:00+00:00
1999-12-31 12:00:00+00:00
1999-12-31 12:00:00+00:00
946641600
UTC forever !!! We cannot change the timezone anymore !?!
Is it PHP or is it me ? Version 5.3.15

Ok, so I was getting mad by myself. Of course, I am the one to be wrong...
To get it straight, I'll just pick up the bits that are relevants in the doc here and here.
Manual says:
// Using a UNIX timestamp. Notice the result is in the UTC time zone.
$date = new DateTime('#946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
So indeed, you can use setTimezone to get times again in your timezone (which could be expected if your system is set up that way !):
$timezone = new DateTimeZone('Europe/Madrid');
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
Note that
$date = new DateTime('#1306123200', new DateTimeZone('Europe/Madrid'));
is misleading, since you will be in UTC anyways ! (and yes, it is very clearly specified in the constructor's doc. So be careful ;)
Thanks #hakre
Thanks all !

It's just you. As far as PHP is concerned, it's all fine and dandy, PHP manual covers this nicely: http://www.php.net/manual/en/datetime.construct.php

Related

Adding date in mysql with timezone

My query is i am adding date in my MySQL table which is now added with CURRENT TIMESTAMP or BY NOW ()... but it takes server time ... i need date/time to be added in my timezone i.e asia/calcutta.
i am trying something but it seems i doing it all wrong
<?php
$today = NOW();
$date = new DateTime($today, new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
can anybody help ??

Format the date according to the time zone of the user in PHP

I need to show the user the date of an event, depending on the time zone you are.
Example ...
If they are at 2014-10-22 11:05:00 (time of event) ...
madrid show 2014-10-22 18:05:00
or
Tokio show 2014-10-23 01:05:00
can you help me?
Thanks
DateTime class is your friend here.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); #What you have
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham')); # What you want to spit out.
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Source

php get local time by timezone code/digit (+1, +2 etc)

Im struggling to find a solution about this,
is there anyway I can use a php or js/jquery
function/plugin to get the local time of a city
or state based on it's timezone code?
For example lets say Rome is timezone 2
Thank you alot.
Please look at the gmstrftime():
<?php
$offset = 2;
$timestamp = time() + ( $offset * 60 * 60 );
echo gmstrftime("%b %d %Y %H:%M:%S", $timestamp) . "\n";
?>
This function formats time against GMT, so you can directly use time zone offsets.
PHP Manual here - http://php.net/manual/en/function.gmstrftime.php
But a more elegant solution could be to use DateTime::setTimezone():
<?php
// From PHP manual
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
I would suggest to look into DateTime class and supported timezones in php.

Convert timezone in php

I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.
ASKT
CDT
EDT
HST
MDT
MST
PDT
How can i convert? Please help
DateTime::setTimezone would help you.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Use the DateTime Class
$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display date
$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again
This, way you don't have to give the same timestamp as new argument every time like mishu's answer.
See the class written by "the_dark_lord12001 at yahoo dot com" this will help you to get timezones from abbreviations & then you can use it
with either date_default_timezone_set or DateTime class
http://www.php.net/manual/en/datetimezone.listabbreviations.php
Hope this help.
~K

PHP manipulate timezone?

If i have date can I add different timezone?
e.g. i have 1/1/2000 5pm in +2gmt i need to add +4gmt = 1/1/2000 7pm
or substract -6gmt from thjis so i will get 1pm?
You could use DateTime and DateTimeZone. There's also a DateInterval class that allows you to specify an interval that you can use with DateTime::add and DateTime::sub.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . PHP_EOL;
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . PHP_EOL;
?>
Outputs:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

Categories