views:

2118

answers:

9

Hi everybody,

i need a help. I am explaining at details so that 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)

so i wanted result from mysql as

Month YEAR

JAN 2009

FEB 2009

MAR 2009

APR 2009

MAY 2009

JUN 2009

JUL 2009

AUG 2009

SEP 2009

OCT 2009

Your help really appreciated.

+4  A: 

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));
}
FractalizeR
This is the right anser for this question.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 $dateMonths; } function GetDateFromMonths ($months) { $years = (int) $months / 12; $month = (int) $months % 12; $myDate = strtotime("$years/$month/1); return $myDate; } (Of course this functions could be rewritten as one liners but wanted to be more readable)
elviejo
A: 

Hi FractalizeR,

First of all thanks for your reply.

if i use mysql statement like this, it reurns no of months as 9. SELECT PERIOD_DIFF(200910, 200901)

So i am looking some simple way may be through mysql statement to get a list of the month and year for specified period.

Please suggest?

Nikhil: can you help me to solve my homework problem

santosh
Please use comments, instead.
Moayad Mardini
Better use PHP. MySQL is for data exatraction. Not for calculations. Issuing a query to MySQL server is a time-consuming operation.
FractalizeR
A: 

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

}

santosh
+5  A: 

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 $dateMonths;
}

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)

elviejo
+2  A: 

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

elviejo
Yes, this is perfect, I think, good stuff to get things done quickly.
santosh
+2  A: 

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));
         }
santosh
That's a bad, but may be working, solution. It involves issuing SQL query and this is time consuming. Better to do all from PHP
FractalizeR
A: 

bart doorwerken!

martyn
A: 

doorwerken omdat, wat denk je dat ik aan het doen ben?

Bart
A: 

Hi Guys, I wrote that funtion base 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;
    }
}
Min Thu Kyaw