Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MySQL timestamp and timezones

If I grab the current timestamp using the NOW() function in MySQL can I grab that field via php and give that time in different time zones? Basically converting current time in the current timezone to another timezone?

like image 600
John Avatar asked Feb 26 '23 11:02

John


1 Answers

You can use the DateTimeZone class:

$gmt = new DateTimeZone("GMT");
$datetimeInGMT = new DateTime($now, $gmt);

It also takes locations in the form continent/city, e.g. Europe/London.

If your datetime is non-UTC, you can use setTimezone:

$datetimeInGMT = new DateTime($now, new DateTimeZone("America/New_York"));
$datetimeInGMT->setTimezone(new DateTimeZone("GMT"));
like image 191
moinudin Avatar answered Mar 08 '23 01:03

moinudin