Search code examples
phptimezonetimestamp-with-timezone

PHP Time Zone Function


I'm new to php. I'm building a web app for an online TV station and I'm working on a program schedule system. My problem is how to display the program time relative to the end user. So that a program that airs at 7am EST on the server would be displayed as 6am CST. Anybody know a simple solution? Thanks


Solution

  • Use the DateTime class, parse the program time in its local timezone then set the timezone to the end user's timezone and display it. Here's a somewhat simple example...

    $dt = new DateTime('7am', new DateTimeZone('America/New_York'));
    echo $dt->format('r');
    // Fri, 03 Oct 2014 07:00:00 -0400
    
    $dt->setTimezone(new DateTimeZone('America/Chicago'));
    echo $dt->format('r');
    // Fri, 03 Oct 2014 06:00:00 -0500