In my recent project there was a requirement to display first day and last day of every month in a given year. So I had written a tiny but useful function in PHP to get array of month in a given year with their first and last day.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php function getFirstDayAndLastDay($year) { $currentYear = $year; $currentYearArray = array(); for ($m=1; $m<=12; $m++) { $getMonthTimeStamp = mktime(0,0,0,$m, 1, $currentYear); $month = date('F', $getMonthTimeStamp); $monthAndYear = strtotime($month."-".$currentYear); $currentYearArray[$month]['MonthStartDate'] = date('Y-m-01', $monthAndYear); $currentYearArray[$month]['MonthEndDate'] = date('Y-m-t', $monthAndYear); } return $currentYearArray; } $firstAndLastDay = getFirstDayAndLastDay(2016); echo "<pre>"; print_r($firstAndLastDay); ?> |
In the above function I am using some PHP built-in date and time functions. Following they are with their description.
mktime(hour,minute,second,month,day,year): is used to find the day for that date and return it in Unix timestamp. This function takes 6 parameters.
date(format,timestamp): is used to format a date and/or a time.
strtotime(time,now): Convert text datetimes into Unix timestamps
Timestamp: is a current time of an event recorded by computer.
Above function takes numeric year (2016) as parameter. Then there is a for loop running 12 times (because there are 12 months in a year). $getMonthTimeStamp converts $m (month) into timestamp. Where $currentYear is a given year. $month converts month’s timestamp into month name. $monthAndYear takes $month and $currentYear as a date and convert them in a UNIX timestamp. $currentYearArray[$month][‘MonthStartDate’] and $currentYearArray[$month][‘MonthEndDate’] is an array key which hold first day and last day of months. Once loop runs 12 time it returns an array $currentYearArray holding first and last day every month.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
Array ( [January] => Array ( [MonthStartDate] => 2016-01-01 [MonthEndDate] => 2016-01-31 ) [February] => Array ( [MonthStartDate] => 2016-02-01 [MonthEndDate] => 2016-02-29 ) [March] => Array ( [MonthStartDate] => 2016-03-01 [MonthEndDate] => 2016-03-31 ) [April] => Array ( [MonthStartDate] => 2016-04-01 [MonthEndDate] => 2016-04-30 ) [May] => Array ( [MonthStartDate] => 2016-05-01 [MonthEndDate] => 2016-05-31 ) [June] => Array ( [MonthStartDate] => 2016-06-01 [MonthEndDate] => 2016-06-30 ) [July] => Array ( [MonthStartDate] => 2016-07-01 [MonthEndDate] => 2016-07-31 ) [August] => Array ( [MonthStartDate] => 2016-08-01 [MonthEndDate] => 2016-08-31 ) [September] => Array ( [MonthStartDate] => 2016-09-01 [MonthEndDate] => 2016-09-30 ) [October] => Array ( [MonthStartDate] => 2016-10-01 [MonthEndDate] => 2016-10-31 ) [November] => Array ( [MonthStartDate] => 2016-11-01 [MonthEndDate] => 2016-11-30 ) [December] => Array ( [MonthStartDate] => 2016-12-01 [MonthEndDate] => 2016-12-31 ) ) |