Created
August 12, 2011 21:20
-
-
Save themattharris/1143040 to your computer and use it in GitHub Desktop.
weekday_diff - calculates the number of weekdays between two 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 | |
/** | |
* Calculated the number of weekdays (M-F) between two timestamps (inclusive). | |
* | |
* @param string $from the timestamp to start measuring from | |
* @param string $to the timestamp to stop measuring at | |
* @param string $normalise whether the time of day should be ignored (forces times to yyyy-mm-ddT00:00:00+00:00) | |
* @return int the number of weekdays between the two timestamps | |
* @author Matt Harris | |
*/ | |
function weekday_diff($from, $to, $normalise=true) { | |
$_from = is_int($from) ? $from : strtotime($from); | |
$_to = is_int($to) ? $to : strtotime($to); | |
// normalising means partial days are counted as a complete day. | |
if ($normalise) { | |
$_from = strtotime(date('Y-m-d', $_from)); | |
$_to = strtotime(date('Y-m-d', $_to)); | |
} | |
$all_days = @range($_from, $_to, 60*60*24); | |
if (empty($all_days)) return 0; | |
$week_days = array_filter( | |
$all_days, | |
create_function('$t', '$d = date("w", strtotime("+{$t} seconds", 0)); return !in_array($d, array(0,6));') | |
); | |
return count($week_days); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Thanks for the share, but running this like
weekday_diff("2018-09-13", "2018-09-13")
gives me a result of 1 instead of the expected 0 -- did I misunderstand the purpose/logic or am I running it wrong somehow?