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
- 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");
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.
source share