Skip to content

Instantly share code, notes, and snippets.

@mrkmg
Created December 10, 2012 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkmg/4253850 to your computer and use it in GitHub Desktop.
Save mrkmg/4253850 to your computer and use it in GitHub Desktop.
Calculate number of weekends per month, quarter, and year
#!/usr/bin/env php
<?php
if(!isset($argv[1]) or !is_numeric($argv[1]) or strlen($argv[1]) != 4){
$cmd = $argv[0];
echo <<<out
See the number of weekend in each month, quarter, and yearly.
Usage: $cmd yyyy
ex: $cmd 2010
ex: $cmd 1999
ex: $cmd 2200
out;
exit(1);
}
$el = PHP_EOL;
$year = $argv[1];
echo "Year:\t".$year.$el.$el;
echo "Item\tNum WE".$el;
$yearDays = 0;
$quaterDays = 0;
$quarter = 1;
for($month=1;$month<=12;$month++){
echo date('M',mktime(0,0,0,$month,1,$year)).":\t";
$daysInMonth = date('t',mktime(0,0,0,$month,1,$year));
$weekendDays = 0;
for($day=1;$day<=$daysInMonth;$day++){
$weekDayNum = date('N',mktime(0,0,0,$month,$day,$year));
if(in_array($weekDayNum,array(5,6,7))){
$weekendDays++;
$quaterDays++;
$yearDays++;
}
}
echo round($weekendDays/3,2).$el;
if(in_array($month,array(3,6,9,12))){
echo 'Q'.$quarter.":\t";
echo round($quaterDays/3,2).$el;
$quaterDays = 0;
$quarter++;
}
}
echo 'Year'.":\t";
echo round($yearDays/3,2).$el;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment