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 ??
Related
I have checked my mysql system timezone with SELECT ##system_time_zone; and its return EDT in production server. Now I want to generate same timezone's (i.e EDT) current timestamp which is not working. This is what i have tried so far is
date_default_timezone_set('EST');
$ts = strtotime('now');
echo date('Y-m-d H:i:s',$ts);
and
$date = new DateTime(null, new DateTimeZone('EST'));
$ts = $date->getTimestamp();
echo date('Y-m-d H:i:s',$ts);
its output is 2015-09-07 05:38:43 but my mysql date (timestamp) is 2015-09-07 06:39:53 so its not same. (tested by inserting same time)
So how can I get current timestamp based on mysql's system time zone (EDT/EST)?? any help from proessional appricated
The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT ##global.time_zone, ##session.time_zone;
And try to use this code to set time zone and Location of time zone
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?
Note second php code is just an example.
I hope I helped you
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
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