Created
November 9, 2015 07:47
-
-
Save chellem/bee0c2268fd24860bba5 to your computer and use it in GitHub Desktop.
Get number of days between 2 dates
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
<?php | |
if (count($argv)<=1) { | |
return usage(); | |
} | |
$format = 'm/d/Y h:i A'; | |
if (isset($argv[3])) { | |
$format = $argv[3]; | |
} | |
$date1 = DateTime::createFromFormat($format, $argv[1]); | |
$date2 = DateTime::createFromFormat($format, $argv[2]); | |
if (!($date1 || $date2)) { | |
echo "\nInvalid input dates or invalid input format\n\n"; | |
return; | |
} | |
$dDiff = $date2->diff($date1); | |
echo $dDiff->format('%R'); | |
echo $dDiff->days." days\n"; | |
function usage() | |
{ | |
echo "\nphp date.php [date1] [date2] [optional:format]\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment