Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active January 29, 2016 06:24
Show Gist options
  • Save sarfraznawaz2005/b8f71985c8de6de4281f to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/b8f71985c8de6de4281f to your computer and use it in GitHub Desktop.
count total monthly hours
<?php
date_default_timezone_set('Asia/Karachi');
#################################################################
$leaves = 0; // edit this if needed
#################################################################
$hours = 8;
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
//loop through all days
for ($i = 1; $i <= $day_count; $i++) {
$date = $year.'/'.$month.'/'.$i; //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
//if not a weekend add day to array
if ($day_name != 'Sun' && $day_name != 'Sat') {
$workdays[] = $i;
}
}
$total_work_days = count($workdays) - $leaves;
$total_hours = (count($workdays) - $leaves) * $hours;
echo "<strong>" . date('F') . ' - ' . $year . "</strong><hr>"
. "Total Working Hours = <strong>$total_hours</strong><br>"
. "Total Working Days = <strong>$total_work_days</strong><br>"
. "Total Days = <strong>$day_count</strong><br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment