Skip to content

Instantly share code, notes, and snippets.

@timkeller
Created February 20, 2012 19:04
Show Gist options
  • Save timkeller/1870762 to your computer and use it in GitHub Desktop.
Save timkeller/1870762 to your computer and use it in GitHub Desktop.
A few quick timezone conversions for a friend (and his co-workers)
<?php
/*
PARSE CSVs OF SHIFT DATA
FORMAT: Note that spaces outside of < >'s are significant. ( )'s indicate optional
<Shift Number>, <start-end>(,<start-end>), <Days of the week off>
eg. Shift 16, 0700-1200,1700-2200, Mon,Tue,Wed off
eg. Shift 102, 0900-1930, Wed,Thu,Sat off
Usage from unix: php thisfile.php > output.xls
Input is assumed in PST. No handling provided for daylight saving time.
*/
// File to open
$fp = fopen("data.txt", "r");
// HTTP Headers - Uncomment if running from a webserver
//$file = "Timezones.xls";
//header("Content-Disposition: attachment; filename=\"$filename\"");
//header("Content-Type: application/vnd.ms-excel");
// Header
echo "Shift\tDays\tPST 24HR\tPST\tMST\tCST\tEST\n";
while (!feof($fp))
{
// Read Line
$buffer = fgets($fp, 4096);
// Break apart
$line_parts = explode(", ", $buffer);
foreach($line_parts as $k=>$v) $line_parts[$k] = trim($v);
$times = explode(",", $line_parts[1]);
// Per shift period
foreach($times as $times_part)
{
list($start_time, $end_time) = explode("-", $times_part);
// Convert to unix time
$unix_start = strtotime($start_time);
$unix_end = strtotime($end_time);
// PST
$date_start["pst"] = mktime(date("H", $unix_start), date("i", $unix_start), date("s", $unix_start));
$date_end["pst"] = mktime(date("H", $unix_end), date("i", $unix_end), date("s", $unix_end));
// Transform +1
$date_start["mst"] = mktime(date("H", $unix_start)+1, date("i", $unix_start), date("s", $unix_start));
$date_end["mst"] = mktime(date("H", $unix_end)+1, date("i", $unix_end), date("s", $unix_end));
// Transform +2
$date_start["cst"] = mktime(date("H", $unix_start)+2, date("i", $unix_start), date("s", $unix_start));
$date_end["cst"] = mktime(date("H", $unix_end)+2, date("i", $unix_end), date("s", $unix_end));
// Transform +3
$date_start["est"] = mktime(date("H", $unix_start)+3, date("i", $unix_start), date("s", $unix_start));
$date_end["est"] = mktime(date("H", $unix_end)+3, date("i", $unix_end), date("s", $unix_end));
// Output
echo $line_parts[0] . "\t" . $line_parts[2] . "\t" .
date("H:i",$date_start["pst"]) . "-" . date("H:i",$date_end["pst"]) . "\t" .
date("h:i a",$date_start["pst"]) . "-" . date("h:i a",$date_end["pst"]) . "\t".
date("h:i a",$date_start["mst"]) . "-" . date("h:i a",$date_end["mst"]) . "\t".
date("h:i a",$date_start["cst"]) . "-" . date("h:i a",$date_end["cst"]) . "\t".
date("h:i a",$date_start["est"]) . "-" . date("h:i a",$date_end["est"]) . "\n";
}
}
// Close reading file
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment