Loop from date to date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
junjustkim
Forum Commoner
Posts: 44
Joined: Thu May 22, 2008 8:48 pm

Loop from date to date

Post 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
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Loop from date to date

Post 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...
Post Reply