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
Asked
Active
Viewed 110 times
0
-
1Hi, welcome to StackOverflow. You'll get a better answer if you include a code sample showing what you've tried already! – user513951 Oct 03 '14 at 04:14
-
Documentation on PHP's date, time, and time zone support can be found [here](http://php.net/manual/en/book.datetime.php). – Matt Johnson-Pint Oct 03 '14 at 04:19
1 Answers
0
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

Phil
- 157,677
- 23
- 242
- 245
-
1Please note that `CST` is not a valid PHP Timezone, but `US/Central` is. – worldofjr Oct 03 '14 at 04:05
-
@worldofjr weird, it works for me and as you can see from my example above, it is correct (-0600). In any case, this is just an example – Phil Oct 03 '14 at 04:30
-
1Not valid though. I think it depends on your server sometimes. See [PHP: Other Timezones](http://php.net/manual/en/timezones.others.php) and you'll notice it's not listed. In any case, those timezones are for backwards compatibility only. EST should use `America/New_York` for example. – worldofjr Oct 03 '14 at 04:32
-
1Also see this SO question [List of US Time Zones for PHP to use?](http://stackoverflow.com/questions/4989209/list-of-us-time-zones-for-php-to-use). – worldofjr Oct 03 '14 at 04:37
-
@worldofjr apparently, you shouldn't use the `US/` prefixed zones either. I've updated my answer with *valid* timezones – Phil Oct 03 '14 at 04:39