Created
December 10, 2012 22:17
-
-
Save mrkmg/4253850 to your computer and use it in GitHub Desktop.
Calculate number of weekends per month, quarter, and year
This file contains 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 | |
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