A PHP function to generate a list of information of all dates for a given month and year.
Input: month and year in number.
Output: Two-dimensional array of all dates.
PHP FUNCTION
/**
* Generate a list of information of all dates for a given month and year.
* @param int $month month number of the year.
* @param int $year 4 digit year number.
* @return array
*
* @author oneTarek http://onetarek.com
**/
function generate_date_list( $month, $year ) {
$list = array();
$month = intval( $month );
if( $month < 0 || $month > 12 ) {
return $list;
}
$year = intval( $year );
if( strlen( $year ) != 4 ) {
return $list;
}
//Find the number of days in a the given month.
$date = date_create_from_format( "j-n-Y", "1-". $month . '-'. $year ); //"9-6-2013"
$num_days = intval( $date->format('t') );
for( $d = 1; $d <= $num_days; $d++ ) {
$date = date_create_from_format( "j-n-Y", $d. "-". $month . '-'. $year ); //"9-6-2013"
$d_arr = array();
$d_arr[ 'd' ] = $date->format( 'd' ); //Day of the month, 2 digits with leading zeros 01 to 31
$d_arr[ 'D' ] = $date->format( 'D' ); //A textual representation of a day, three letters Mon through Sun
$d_arr[ 'j' ] = $date->format( 'j' ); //Day of the month without leading zeros 1 to 31
$d_arr[ 'l' ] = $date->format( 'l' ); //A full textual representation of the day of the week Sunday through Saturday
$d_arr[ 'F' ] = $date->format( 'F' ); //A full textual representation of a month, such as January or March January through December
$d_arr[ 'm' ] = $date->format( 'm' ); //Numeric representation of a month, with leading zeros 01 through 12
$d_arr[ 'M' ] = $date->format( 'M' ); //A short textual representation of a month, three letters Jan through Dec
$d_arr[ 'n' ] = $date->format( 'n' ); //Numeric representation of a month, without leading zeros 1 through 12
$d_arr[ 'Y' ] = $date->format( 'Y' ); //A full numeric representation of a year, at least 4 digits, with - for years BCE.
$d_arr[ 'y' ] = $date->format( 'y' ); //A two digit representation of a year Examples: 99 or 03
$d_arr[ 'full' ] = $date->format( 'l, F d, Y' ); //Friday, June 09, 2023
$list[ $d ] = $d_arr;
}
return $list;
}
CALL THE FUNCTION:
<?php
$list = generate_date_list( 6, 2023 );
echo "<pre>"; print_r( $list ); echo '</pre>';
?>
OUTPUT:
Array
(
[1] => Array
(
[d] => 01
[D] => Thu
[j] => 1
[l] => Thursday
[F] => June
[m] => 06
[M] => Jun
[n] => 6
[Y] => 2023
[y] => 23
[full] => Thursday, June 01, 2023
)
.....
.....
.....
[30] => Array
(
[d] => 30
[D] => Fri
[j] => 30
[l] => Friday
[F] => June
[m] => 06
[M] => Jun
[n] => 6
[Y] => 2023
[y] => 23
[full] => Friday, June 30, 2023
)
)