Page 1 of 1

Loop from date to date

Posted: Thu May 28, 2009 9:29 pm
by junjustkim
hi to all

how to use from date to date for looping.

my date format (YY/MM/DD)
I have a date from 2009/01/05 to 2010/03/20

I want to execute into my sql query are

Code: Select all

select * from mytable where date_created between 2009/01/05 and 2009/01/31 //for 2009/01 (january 2009) records
then next loop:

Code: Select all

select * from mytable where date_created between 2009/02/01 and 2009/02/31 //for 2009/01 (february 2009) records
until to satisfied "to date (2010/03/20)";

any suggestion would greatly appreciated

thanks in advance

Tirso

Re: Loop from date to date

Posted: Thu May 28, 2009 9:38 pm
by mischievous
I prefer using a unix timestamp...

Code: Select all

 
$fromDate = 1231113600; //=1/05/2009
 
$toDate = 1233360000; //= 1/31/2009 
 
$fromDate -= 1; //subtract one for between statement
$toDate +=1; //add one for between statement
Query:

Code: Select all

$myQuery = "SELECT * FROM mytable WHERE created_date BETWEEN $fromDate AND $toDate";
$myQueryResult = mysql_query($myQuery);
 
foreach($myQueryResult->result() as $row)
{
echo ($row->fieldname);
}

Not Sure if that helps...