SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Feb 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP function for Time Zone conversion

    I recently found that PHP doesn't have an inbuilt function for conversion of time/date between multiple time zones and neither could I find a third-party function for the purpose. However, I did find the PEAR class which has inbuilt support for multiple time-zones but it cannot be used by "including" itself in the php page. PEAR class can be used only after installation, which may not be feasible in each and every case.
    To avoid the hassles of installation I have written a set of 3 functions in PHP which wil allows you to -

    1. Convert GMT to local time zone
    2. Convert local time zone to GMT
    3. Convert between two different time zones.
    Last edited by DaveMaxwell; Mar 24, 2010 at 09:06. Reason: removed link

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Is this an advertisement or are you looking for a discussion on the topic?

    Here's how I would change the timezone:

    PHP Code:
    // Create a datetime with time zone set to UTC
    $dt = new DateTime("2010-03-24 12:34", new DateTimezone("UTC"));
    var_dump($dt->format('r'));

    // Change the time zone to LA
    $dt->setTimezone(new DateTimezone("America/Los_Angeles"));
    var_dump($dt->format('r')); 
    Which outputs:
    Code:
    string(31) "Wed, 24 Mar 2010 12:34:00 +0000"
    string(31) "Wed, 24 Mar 2010 05:34:00 -0700"
    Salathe
    Software Developer and PHP Manual Author.


Tags for this Thread

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
  •