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$%^#!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.