Jump to content

for loop to increment day by one each iteration not working as expected


OldWest

Recommended Posts

I am doing some study on using time() and date() and i just wrote this simple for loop to add one day to each iteration with +$i and its echoing the unix timestamp as opposed to the correctly formated date as it should be based on my code. Anyone have any idea why this is not working as expected?

 

for($i=0; $i < 50; $i++)
{
echo $time = time()+$i . "<br />"; // add a day on each iteration
echo date('y-m-d', $time) . "<br />"; // should echo 10-12-02, 10-12-03, 10-12-04, etc..
}

 

what am i doing wrong here? arrgggg! maybe its too late for this s$%^#!

 

If you want to do this using your original concept, you need to increment $i by 86400, since the time function returns seconds and there are 86400 seconds in a day (except when DST starts/ends).

 

<?php
$end = 50 * 86400;
for ($i=0;$i< $end; $i += 86400) {
   $time = time() + $i;
   echo date('y-m-d', $time) . "<br />";
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.