Question

Im struggling to find a solution about this, is there anyway I can use a php or js/jquery function/plugin to get the local time of a city or state based on it's timezone code? For example lets say Rome is timezone 2

Thank you alot.

Was it helpful?

Solution

Please look at the gmstrftime():

<?php
    $offset = 2;
    $timestamp = time() + ( $offset * 60 * 60 );
    echo gmstrftime("%b %d %Y %H:%M:%S", $timestamp) . "\n";
?>

This function formats time against GMT, so you can directly use time zone offsets.

PHP Manual here - http://php.net/manual/en/function.gmstrftime.php

But a more elegant solution could be to use DateTime::setTimezone():

<?php
// From PHP manual 

$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";
?>

OTHER TIPS

I would suggest to look into DateTime class and supported timezones in php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top