Created
May 11, 2010 23:49
-
-
Save lox/398039 to your computer and use it in GitHub Desktop.
Command-line script for manipulating timestamps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Command-line script for manipulating timestamps | |
*/ | |
// parse options | |
$options = array('f'=>'r', 't'=>@date_default_timezone_get()); | |
foreach(array_keys($options) as $arg) | |
{ | |
if(($idx = array_search("-$arg", $argv)) !== false) | |
$options[$arg] = $argv[$idx+1]; | |
} | |
// filter down to arguments | |
$args = preg_grep('/^-(f|u|t|)/', array_slice($argv,1), PREG_GREP_INVERT); | |
// show help | |
if(in_array('-h', $argv) || in_array('--help', $argv)) | |
{ | |
echo "\nusage: $argv[0] [datestring] [-f <format>] [-t timezone] [-u]\n\n"; | |
echo " -f fmt : a php format string\n"; | |
echo " -t tz : a timezone string\n"; | |
echo " -u : output a bare timestamp\n\n"; | |
exit(1); | |
} | |
$string = isset($args[0]) ? $args[0] : '@'.time(); | |
$time = new DateTime($string, new DateTimeZone($options['t'])); | |
if(in_array('-u',$argv)) | |
printf("%d\n", $time->getTimestamp()); | |
else | |
printf("%s => [%d]\n",$time->format($options['f']), $time->getTimestamp()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment