Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Last active November 20, 2023 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BhargavBhandari90/c7d2f75b93cdbd462db330ab7ba41ca1 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/c7d2f75b93cdbd462db330ab7ba41ca1 to your computer and use it in GitHub Desktop.
Get total number of weekdays in a particular year.
<?php
/**
* Get total number of weekdays in a year.
*
* @param string $day Week day. Sun, Mon, Tue,...
* @param string $year Year in 4 digits.
* @return integer Return total number of specified weekday for a year.
*/
function get_total_weekday_in_year( $day = '', $year = '' ) {
// Bail, if anything goes wrong.
if ( empty( $day ) ) {
return;
}
// Set current year.
if ( empty( $year ) ) {
$year = date( 'Y' );
}
$fromdt = date( 'Y-m-01 ', strtotime( "First Day Of January $year" ) );
$todt = date( 'Y-m-31 ', strtotime( "Last Day of December $year" ) );
$iter = DAY_IN_SECONDS; // whole day in seconds
$count = 0; // keep a count of Weekday
for ( $i = strtotime( $fromdt ); $i <= strtotime( $todt ); $i = $i + $iter ) {
if ( Date( 'D', $i ) == $day ) {
$count++;
}
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment