A PHP script to display the time in a given location

Jan 30, '04 09:28:00AM

Contributed by: Thom

Okay, so I can never keep track of timezones, etc. I realize there may very well be a utility to do this but I'm not aware of what it is. So I wrote a quick one. It's just a php wrapper to the command:

% TZ=":Asia/Taipei" date

You can find all of the timezones in /usr/share/zoneinfo.

I put this script in my ~/bin, called it timein and made it executable (chmod 755 timein):


#!/usr/bin/php
<?php  $city = $_SERVER['argv'][1];
$findresult = `find /usr/share/zoneinfo -iname $city`;

if (strlen($findresult) == 0)
{
  print "Sorry, city not found.\n";
  exit();
}

$tzarray = split("\n", $findresult);
$tzfile = $tzarray[0];

$tzstring = ":" .  str_replace("/usr/share/zoneinfo/", "",$tzfile);

print `TZ="$tzstring" date`;
print "\n";
?>
Usage is simple:
% timein london
Thu Jan 22 05:16:53 GMT 2004
If you wanted to add a city/state that isn't found (but you know of a neighboring file that is), you could probably either copy the file or softlink it in the terminal, e.g.:
% cd /usr/share/zoneinfo/US
share/zoneinfo/US% ls -la  (trimmed down)
-rw-r--r--   4 root  wheel   130 25 Sep 17:46 Hawaii
-rw-r--r--   3 root  wheel   549 25 Sep 17:46 Indiana-Starke
-rw-r--r--   2 root  wheel   811 25 Sep 17:46 Michigan

share/zoneinfo/US% sudo ln -s Michigan Flint
share/zoneinfo/US% ls -la Flint
lrwxr-xr-x   1 root  wheel     8 21 Jan 23:21 Flint -> Michigan

share/zoneinfo/US% timein flint
Thu Jan 22 00:21:40 EST 2004
Hmm, guess it's a bad time to call my folks...

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040122002707402