Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I loop through a series of dates 1

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
I have a start date and an end date I want to loop through each month between those dates and I can't figure out how to do it currently I am echoing my results to the screen to confirm I am getting the right data.

here is my code
Code:
while ($e->fetch())
{
	$start = strtotime($e->install_date);
	$today = strtotime(date('Y-m-d'));
	$oeid = $e->Original_Equipment_id;

	for ($d = $start; $d <= $today; strtotime($d.' + 1 month'))
	{
		echo 'equpipment id: '.$oeid.', date: '.date('Y-m', $d).'<br>';
	}
}
the problem is the results only ever return the same value. it does not seem to increament at all.
1, 2004-11
1, 2004-11
1, 2004-11
1, 2004-11
1, 2004-11

any ideas?

Jason Meckley
Web Application Developer
SSIC
 
There are a number of errors in your code. Here's some code that does what you want.
Code:
<?
    $start = strtotime('2004-11-1');
    $today = strtotime('today');

    for ($d = $start; $d <= $today; $d=strtotime(' + 1 month',$d))
    {
        echo 'date: ' . date('Y-m', $d).'<br>'."\n";
    }
?>
Explanation:
For today's date, use the word "today" with strtotime.
In your "for loop", you never increment $d. I increment it by telling strtotime to add 1 month to the current value of $d.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top