I want to get current time of US/Eastern timezone. How would I achieve that.
I have tried following code but it is displaying my system's time.
<?php
date_default_timezone_set('US/Eastern');
$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
//print "&time2=".$secs."&time1=".$mins."&time0=".$hrs;
?>
I am using this script with flash so commented out 'print' line.
The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.
The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.
<?php
echo date_default_timezone_get();
$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
echo " => $hrs:$mins:$secs\n";
date_default_timezone_set('US/Eastern');
echo date_default_timezone_get();
$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
echo " => $hrs:$mins:$secs\n";
date_default_timezone_set('America/New_York');
echo date_default_timezone_get();
$currenttime = date('h:i:s:u');
list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
echo " => $hrs:$mins:$secs\n";
?>
Seems to work here (in Berlin):
Europe/Berlin => 01:42:42
US/Eastern => 07:45:18
America/New_York => 07:45:18
$amNY = new DateTime('America/New_York');
$estTime = $amNY->format('h:i:s:u');
or if you are using php 5.4 and above
estTime = (new DateTime('America/New_York'))->format('h:i:s:u');
date_default_timezone_set()
will affect the whole script and should be used carefully
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With