To get all dates in given month and year, we need to know how many days in that given month and year.
to do that we are going to use PHP's date() and mktime() functions.
Syntax :
date(format,timestamp)
mktime(hour,minute,second,month,day,year,is_dst);
Function:
the following function will return dates in array format for given month and year in Y-m-d format.
Parameters:
usage:
Example:
output:
Explanation:
We just calculated the number of days in month and using for loop, we are creating array in our own format.
Have any doubt, feel free to comment here!
to do that we are going to use PHP's date() and mktime() functions.
Syntax :
date(format,timestamp)
mktime(hour,minute,second,month,day,year,is_dst);
Function:
the following function will return dates in array format for given month and year in Y-m-d format.
This file contains hidden or 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
/*Get All Dates in given month and year in php by CJ Ramki (http://cj-ramki.blogspot.in)*/ | |
function get_dates($month,$year){ | |
$numbers = array('1','2','3','4','5','6','7','8','9'); | |
$datesArray = array(); | |
$num_of_days = date('t',mktime (0,0,0,$month,1,$year)); | |
for( $i=1; $i<= $num_of_days; $i++) { | |
if(in_array($i,$numbers)) $i = '0'.$i; | |
//complete date in Y-m-d format | |
$datesArray[]= $moYe[1] . "-". $moYe[0]. "-". $i; | |
} | |
return $datesArray; | |
} |
$month - Month number
$year - Year
usage:
$dates = get_dates($month,$year);
Example:
This file contains hidden or 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
$dates = get_dates(6, 2014); | |
echo '<pre>'; | |
print_r($dates); |
This file contains hidden or 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
Array | |
( | |
[0] => 2014-6-01 | |
[1] => 2014-6-02 | |
[2] => 2014-6-03 | |
[3] => 2014-6-04 | |
[4] => 2014-6-05 | |
[5] => 2014-6-06 | |
[6] => 2014-6-07 | |
[7] => 2014-6-08 | |
[8] => 2014-6-09 | |
[9] => 2014-6-10 | |
[10] => 2014-6-11 | |
[11] => 2014-6-12 | |
[12] => 2014-6-13 | |
[13] => 2014-6-14 | |
[14] => 2014-6-15 | |
[15] => 2014-6-16 | |
[16] => 2014-6-17 | |
[17] => 2014-6-18 | |
[18] => 2014-6-19 | |
[19] => 2014-6-20 | |
[20] => 2014-6-21 | |
[21] => 2014-6-22 | |
[22] => 2014-6-23 | |
[23] => 2014-6-24 | |
[24] => 2014-6-25 | |
[25] => 2014-6-26 | |
[26] => 2014-6-27 | |
[27] => 2014-6-28 | |
[28] => 2014-6-29 | |
[29] => 2014-6-30 | |
) |
Explanation:
We just calculated the number of days in month and using for loop, we are creating array in our own format.
Have any doubt, feel free to comment here!
0 comments:
Post a Comment