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
Related
$row['created'] <- a variable get date/time into Database .
$row['created'] have time like 2016-10-07 01:31:42 . and time zone(America/Chicago) .
so I would like show time according Indian time zone . I am using code shown in below but is not working.
$timestamp = strtotime($row['created']);
date_default_timezone_set("Asia/Kolkata");
echo date('r', $timestamp);
Try this code to save as indian time.
<?php
$date=date_create('2016-10-07 01:26:56',timezone_open("America/Chicago"));
echo date_format($date,"Y-m-d H:i:sP")."<br>";
date_timezone_set($date,timezone_open("Asia/Kolkata"));
$indiatime = date_format($date,"Y-m-d H:i:sP") . "<br>";
echo $indiatime ;
?>
You can use DateTime class. Look at this DateTime doc example.
$date = new DateTime($row['created'], new DateTimeZone('America/Chicago'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:sP') . "\n";
This will let you jump between timezones.
Try this, use timezone_open and date_format
$timestamp = "2016-10-07 01:31:42";
$date = date_create($timestamp, timezone_open('Asia/Kolkata'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n"; //echo 2016-10-07 01:31:42+05:30
or
$timestamp = "2016-10-07 01:31:42";
$user_tz = 'Asia/Kolkata';
echo $timestamp. "\n"; // echo 2016-10-07 01:31:42
$schedule_date = new DateTime($timestamp, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn; // echo 2016-10-06 20:01:42
DEMO
If you are using mysql, you do this also:
SELECT CONVERT_TZ(created,'America/Chicago','Asia/Kolkata');
Instead of coding in php, when you fetch data from database you may be use the following query: select field1,field2,created from <tablename> where <yourCondition>. Instead of doinf so update your query this way:
select field1,field2,CONVERT_TZ(created,'America/Chicago','Asia/Kolkata') from <tablename> where <yourCondition>
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 ??
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
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
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