List all months and years between two dates in PHP

I am trying to explain the details so that it is easy to understand.

I want a list of the month and year based on the difference of the month and year.

I implement a search function based on the month of the beginning and end of the month. So!

start Selection - 01 (month) -2009 (year)

Final selection 10 (month) -2009 (year)

What I want from MySQL:

Month Year JAN 2009 FEB 2009 MAR 2009 APR 2009 MAY 2009 JUN 2009 JUL 2009 AUG 2009 SEP 2009 OCT 2009 
+4
source share
7 answers

FractalizeR's answer is correct.

Just let me expand by defining functions:

 function GetMonthsFromDate($myDate) { $year = (int) date('Y',$myDate); $months = (int) date('m', $myDate); $dateAsMonths = 12*$year + $months; return $dateAsMonths; } function GetDateFromMonths($months) { $years = (int) $months / 12; $month = (int) $months % 12; $myDate = strtotime("$years/$month/01"); //makes a date like 2009/12/01 return $myDate; } 

PS: tried to post a comment, but the formation was screwed. (Of course, these functions could be rewritten as one liner, but I would like to be more readable)

+9
source

You need to write some functions to convert your dates into several months elapsed from a specific date and back. For example, since January 1980.

 Jan 1980 = 0; Dec 1980 = 12; Jan 1981 = 13 etc. 

Then you just do a simple "for" loop:

 for ($i = GetMonthsFromDate($StartDate), $i <= GetMonthsFromDate($StopDate), $i++) { echo(GetDateFromMonths($i)); } 
+8
source

Although the answer of FractalizeR is correct. There is another option.

Taking advantage of the fact that strtotime ('2009/08/01 - 1 month) will do the right thing and delete 1 month.

 <?php $startDate = strtotime("$startYear/$startMonth/01"); $endDate = strtotime("$endYear/$endMonth/01"); $currentDate = $endDate; while ($currentDate >= $startDate) { echo date('Y/m',$currentDate); $currentDate = strtotime( date('Y/m/01/',$currentDate).' -1 month'); } 

List of months again

+8
source

Here is the final answer that works great

 $startMonth= $_POST['startmonth']; $startyear= $_POST['startyear']; $cYear = $startyear; $endMonth= $_POST['endmonth']; $endyear= $_POST['endyear']; $sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")"; $queryRS = $db->query($sql); $tmonthsarray = $db->fetchRow($c_jobsVat); $totalmonths=tmonthsarray[0]; for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++) { $processYear = $startyear + intval( ( $count - 1 ) / 12 ); $processMonth= (( $count - 1 ) % 12 + 1); $processMonthName= date('F', mktime(0,0,0,$count)); } 
+2
source

I wrote this feature based on you guys. Check this:

 function CountTheMonth($startDate,$endDate,$order) { $startDate = strtotime($startDate); $endDate = strtotime($endDate); $ASC_Month = $startDate; $DESC_Month = $endDate; $Y_Axis = Array(); if($order == 'DESC')//Big to small { while ($DESC_Month >= $startDate) { $Y_Axis[] = date('F-Y',$DESC_Month); $DESC_Month = strtotime( date('Ym-d',$DESC_Month).' -1 month'); } return $Y_Axis; } elseif($order == 'ASC')//Small to big { while ($ASC_Month <= $endDate) { $Y_Axis[] = date('F-Y',$ASC_Month); $ASC_Month = strtotime( date('Ym-d',$ASC_Month).' +1 month'); } return $Y_Axis; } } 
+1
source

Your logic works great to get a list of months. But not sure how we can handle the years in this case.

Here is my code:

 $startMonth= $_POST['startmonth']; $startyear= $_POST['startyear']; $endMonth= $_POST['endmonth']; $endyear= $_POST['endyear']; $sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")"; $queryRS = $db->query($sql); $tmonthsarray = $db->fetchRow($c_jobsVat); $totalmonths= $tmonthsarray[0]; for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++) { echo "<BR>==>".date('F', mktime(0,0,0,$count)) ; // Months // what comes here in case of listing year } 
0
source

I definitely agree with the @FractalizeR solution and the addition by @elviejo, this helped me move forward. But I believe that I encountered some problems, and they can help others: (+, please correct me if I am wrong)

  • It is important to note: both $StartDate and $StopDate are of type timestamp , not date -type (1) . Therefore, I changed the function parameters to $myTimestamp in the hope of not confusing the others.

For instance:

 $StartDate = strtotime("Sept 2010"); $StopDate = time(); // current timestamp 
  1. For @elviejo functions:
    • typecasting: simple (int) didn't work for me (PHP5), so I used intval() instead
    • Working with % gives results from 0 to 11, and months are in the range from 1 to 12 (2) . So, in the GetDateFromMonths($months) function, we should +1 get the result
    • Since we add 1 to % to the GetDateFromMonths($months) function, we will have to subtract 1 from $dateAsMonths in the GetMonthsFromDate($myTimestamp) function.

This gives me the following code:

 function GetMonthsFromDate($myTimestamp) { $year = (int) date('Y',$myTimestamp); $months = (int) date('m', $myTimestamp); $dateAsMonths = 12*$year + $months - 1; return $dateAsMonths; } function GetDateFromMonths($months) { $years = intval($months / 12); $month = intval($months % 12) + 1; $myTimestamp = strtotime("$years/$month/01"); //makes a date like 2009/12/01, and translate it to a timestamp return $myTimestamp; } 

The following for -loop is launched:

 for ($i = GetMonthsFromDate($StartDate); $i <= GetMonthsFromDate($StopDate); $i++) { echo(date('d M Y',GetDateFromMonths($i))."</br>"); } 

This gave the desired result. Do my comments make sense?


Notes:

(1) I don’t even think that date exists as type in PHP , but I may be wrong.

(2) January 1, February - 2, ... November - 11, but December 0 . This will not give the desired result.

0
source

All Articles