Skip to content

Instantly share code, notes, and snippets.

@themattharris
Created August 12, 2011 21:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themattharris/1143040 to your computer and use it in GitHub Desktop.
Save themattharris/1143040 to your computer and use it in GitHub Desktop.
weekday_diff - calculates the number of weekdays between two dates.
<?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);
}
?>
@Req
Copy link

Req commented Sep 13, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment