-3

My timezone name is :America/Belem I need this time,

https://www.google.co.in/?gfe_rd=cr&ei=ScBlVdS6GLTv8weE8YH4BQ&gws_rd=ssl#q=Bel%C3%A9m+time My code is

$MNTTZ = new DateTimeZone('America/Belem');
 $dt = new DateTime("now", $MNTTZ);
                    $minutes = $dt->format('i');
                    $dayhour = $dt->format('H');
                    $hour = $dt->format('h');
                    $ampm = $dayhour >= 12 ? 'PM' : 'AM';
                    //$min =  $minutes < 10 ? '0'.$minutes : $minutes;
                    $strTime = $hour . ':' . $minutes . ' ' . $ampm;
5
  • Why not just use the DateTime object's format() method to format the time?
    – Mark Baker
    May 27, 2015 at 14:08
  • We need a particular city local time. So i used like above. Here i have city timezone name(America/Belem) only in my db. That's why. In my above code execution i received 1 hour difference compare with google. May 27, 2015 at 14:11
  • No! I mean you have 5 lines of code there, extracting hours, minutes, working out AM/PM, etc...... format() can do that for you in one simple method call
    – Mark Baker
    May 27, 2015 at 14:12
  • could you please simplify the code? I need exact time in final. Please! May 27, 2015 at 14:14
  • Please check here google.co.in/… And execute below code also. You'll get one hour difference. check it. I need exact timezone! May 27, 2015 at 14:21

2 Answers 2

3
$timezone="America/Belem";
date_default_timezone_set($timezone);
$date = new DateTime($datetime, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone($timezone));

After this, you can change the date in whatever format you want.

E.g: $date->format("Y-m-d H:i:s")
4
  • Please check here google.co.in/… And execute your code also. You'll get one hour difference. check it May 27, 2015 at 14:18
  • Maybe daylight saving time makes the difference.
    – peszo
    May 27, 2015 at 14:26
  • @vijayraman you can test here timezoneconverter.com/cgi-bin/zoneinfo?tz=America/Belem Also, you are asking for "America/Belem" whose timezone is -3, while timezone for "Brazil" that you are checking is -4 May 27, 2015 at 14:33
  • yes you're correct. How can we take the daylight in time calculation on above code! May 27, 2015 at 14:48
0

There are no future Daylight Saving Time rules for this time zone. I'm using php's list of supported timezone, by using PHP's date_default_timezone_set, my only concern is the DST. I've found a method which works using PHP's DateTimezone class..

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    $trans = $tz->getTransitions();
    return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}

The getTransitions() function gives you information about each time the offset changes for a timezone.

var_dump(timezoneDoesDST("America/Belem"));//Return bool(false);

var_dump(timezoneDoesDST("US/Pacific")); //Return bool(true);
var_dump(timezoneDoesDST("Europe/London")); //Return bool(true);
var_dump(timezoneDoesDST("America/Chicago")); //Return bool(true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.