Skip to content
Advertisement

Change time zone of gmdate PHP function

I am using the gmdate PHP function to insert the current date to the database but it shows GMT time , can I change it to my time zone +3 ?

this is the code

$sql = "INSERT INTO Students
VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "', 
        '" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "', 
        '" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] . "',
        '". gmdate('m/d/Y g:i:s A') . "')" ;

Advertisement

Answer

“+3” is not a “timezone”. It’s an offset from UTC/GMT, which may change throughout the year based on DST settings. A timezone is something like “Europe/Berlin” or “Asia/Tokyo”.

Once you have decided on a real timezone:

date_default_timezone_set('Europe/Berlin');
echo date('m/d/Y g:i:s A');

or

$date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
echo $date->format('m/d/Y g:i:s A');

If all you have to work with is “+3” then a) you’re screwed and b) you can simply add “3 hours” to any timestamp:

date_default_timezone_set('UTC');

echo date('m/d/Y g:i:s A', strtotime("$offset hours"));

echo date('m/d/Y g:i:s A', time() + ($offset * 60 * 60));

$date = new DateTime;
$date->modify("$offset hours");
echo $date->format('m/d/Y g:i:s A');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement