I'm storing the UTC dates into the DB using:
$utc = gmdate("M d Y h:i:s A");
and then I want to convert the saved UTC date to the client's local time.
How can I do that?
Thanks
I'm storing the UTC dates into the DB using:
$utc = gmdate("M d Y h:i:s A");
and then I want to convert the saved UTC date to the client's local time.
How can I do that?
Thanks
Here is a straight way to convert the UTC time of the questioner to local time. This is for a stored time in a database etc., i.e. any time. You just have to find the time difference between UTC time and the local time you are interested in and then ajust the stored UTC time adding to it the difference.
You can then add this difference to the stored UTC time. (In my place, Athens, the difference is exactly 5:00:00)
Example:
Here I am sharing the script, convert UTC timestamp to Indian timestamp:-
PHP's
strtotime
function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code
""+(new Date())
gives):Or if you don't, which is likely from MySQL, then:
First, get the date in UTC -- you've already done that so this step would really just be a database call:
Next, set your local time zone in PHP:
And now get the offset in seconds:
Finally, add the offset to the integer timestamp of your original datetime:
date()
andlocaltime()
both use the local timezone for the server unless overridden; you can override the timezone used withdate_default_timezone_set()
.http://www.php.net/manual/en/function.date-default-timezone-set.php
http://us3.php.net/manual/en/function.date.php
http://php.net/manual/en/function.localtime.php