OldWest Posted December 2, 2010 Share Posted December 2, 2010 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 https://forums.phpfreaks.com/topic/220451-for-loop-to-increment-day-by-one-each-iteration-not-working-as-expected/ Share on other sites More sharing options...
OldWest Posted December 2, 2010 Author Share Posted December 2, 2010 SOLVED: for($i=0; $i < 50; $i++) { echo date('D, F jS, Y', strtotime("+$i days")) . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/220451-for-loop-to-increment-day-by-one-each-iteration-not-working-as-expected/#findComment-1142141 Share on other sites More sharing options...
kenrbnsn Posted December 2, 2010 Share Posted December 2, 2010 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 https://forums.phpfreaks.com/topic/220451-for-loop-to-increment-day-by-one-each-iteration-not-working-as-expected/#findComment-1142259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.