SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new DateTimeZone Question

    I am having problem using the new DateTimeZone function. Here is my code:

    PHP Code:
    $userTimezone = new DateTimeZone('America/New_York');
    $gmtTimezone = new DateTimeZone('America/Los_Angeles');
    $myDateTime = new DateTime('2014-02-19 15:00'$gmtTimezone);
    $offset $userTimezone->getOffset($myDateTime);
    echo 
    $offset;
    echo 
    date('h:i a'$myDateTime->format('U') + $offset); 
    It outputs:
    -180000
    1:00 pm

    However, the offset seems to always be off 1 hour. It should be listing 12:00 pm. Any ideas why this may be happening?

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't think you need to calculate anything like that. You can just set the time zone on your date object and it will convert the time for you.
    PHP Code:
    $userTimezone = new DateTimeZone('America/New_York'); 
    $gmtTimezone = new DateTimeZone('America/Los_Angeles'); 
    $myDateTime = new DateTime('2014-02-19 15:00'$gmtTimezone);
    echo 
    $myDateTime->format'h:i a' ); // 03:00 pm

    $myDateTime->setTimezone$userTimezone );
    echo 
    $myDateTime->format'h:i a' ); // 06:00 pm 
    (note that your variable is called gmt but isn't. that's why it is not showing 12:00 pm)
    - Robert

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by wickedneat View Post
    .....
    However, the offset seems to always be off 1 hour. It should be listing 12:00 pm. Any ideas why this may be happening?
    A daylight savings time thing?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •