Last active
November 20, 2023 05:49
-
-
Save BhargavBhandari90/c7d2f75b93cdbd462db330ab7ba41ca1 to your computer and use it in GitHub Desktop.
Get total number of weekdays in a particular 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
<?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