Reputation: 132
I am trying to explain the details so that this is easy to understand.
I want a list of month and year based on the difference of month and year.
I am implementing search functionality based on start month with year and end month with year. So!
start Selection - 01(month)-2009 (Yr)
End selection 10(month)-2009 (Yr)
What I want from MySQL is:
Month Year
JAN 2009
FEB 2009
MAR 2009
APR 2009
MAY 2009
JUN 2009
JUL 2009
AUG 2009
SEP 2009
OCT 2009
Upvotes: 4
Views: 18341
Reputation: 547
I definitely agree with the solution of @FractalizeR and the addition by @elviejo , it helped me forward. But I believe I encountered some issues, and they might of help to others: (+ please correct me if I am wrong)
$StartDate
and $StopDate
are of type timestamp
, not actually the date
-type (1). So I changed the function parameters to $myTimestamp
in the hope of not confuse others.For example:
$StartDate = strtotime("Sept 2010");
$StopDate = time(); // current timestamp
(int)
didn't work for me (PHP5), so I used intval()
instead%
gives results ranging from 0 to 11, while months are in the range of 1 to 12 (2). So in the function GetDateFromMonths($months)
, we have to +1
the result%
in the function GetDateFromMonths($months)
, we will have to substract 1 from the $dateAsMonths
in the function GetMonthsFromDate($myTimestamp)
.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;
}
Started by the following for
-loop:
for ($i = GetMonthsFromDate($StartDate); $i <= GetMonthsFromDate($StopDate); $i++)
{
echo(date('d M Y',GetDateFromMonths($i))."</br>");
}
Which gave the wanted result. Do my remarks make sense?
Notes:
(1) I don't even think date
exists as a type in PHP, but I might be wrong.
(2) January is 1, February is 2, ... November is 11 but December is 0. This will not give the wanted result.
Upvotes: 0
Reputation: 132
Your logic works great to get a list of months. But not sure how we can deal with 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
}
Upvotes: 0
Reputation: 11
I wrote that function based on you guys's input. Check this out:
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('Y-m-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('Y-m-d',$ASC_Month).' +1 month');
}
return $Y_Axis;
}
}
Upvotes: 1
Reputation: 4600
FractalizeR answer is the right one.
Just let me expand by defining the 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 as a comment but the formating got screwed. (Of course this functions could be rewritten as one liners but wanted to be more readable)
Upvotes: 9
Reputation: 4600
Although FractalizeR answer is the correct one. There is another option.
Taking advantage from the fact that 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');
}
Again a list of months
Upvotes: 8
Reputation: 132
Here is the final answer which is worked very 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));
}
Upvotes: 2
Reputation: 29965
You need to write some functions to convert your dates to a number of months passed since certain date and back. For example, since Jan 1980.
Jan 1980 = 0;
Dec 1980 = 12;
Jan 1981 = 13 etc.
Then you just do simple "for" loop:
for ($i = GetMonthsFromDate($StartDate), $i <= GetMonthsFromDate($StopDate), $i++) {
echo(GetDateFromMonths($i));
}
Upvotes: 8