coding_begins
09-13-2011, 05:13 PM
how can I print the last thirty dates. Starting from today:
Example:
2011 09 13
2011 09 12
......
2011 08 14
Thanks..
Example:
2011 09 13
2011 09 12
......
2011 08 14
Thanks..
|
||||
display 30 dates starting from todaycoding_begins 09-13-2011, 05:13 PM how can I print the last thirty dates. Starting from today: Example: 2011 09 13 2011 09 12 ...... 2011 08 14 Thanks.. Inigoesdr 09-13-2011, 05:19 PM for($i = 0; $i < 30; $i++) echo date('Y m d', strtotime('today + ' . $i . ' day')), '<br />'; coding_begins 09-13-2011, 07:00 PM I am printing the last 30 days starting from today. i want to print the current date and also the the day after the current date in the next line. The below code does not seem to work correctly.. for($i = 0; $i < 30; $i++) { $d[$i]=strtotime('today - ' . $i . ' day'); echo "D:".date('Y m d', $d[$i])."<br>"; echo "D+1:".date('Y m d', $d[$i+1])."<br>"; } coding_begins 09-13-2011, 07:01 PM I mean it odes not print the D+1 value correctly.. jimhill 09-13-2011, 07:20 PM You say the day after. Do you mean one more day back or one day forward? This shows the next day back, change the -1 to +1 if you want the other way. <?php for($i = 0; $i < 30; $i++) { $y=$i+1; $d[$i]=strtotime('today - ' . $i . ' day'); $d1[$i]=strtotime('today - ' . $y . ' day'); echo "D:".date('Y m d', $d[$i])."<br>"; echo "D+1:".date('Y m d', $d1[$i])."<br>"; } ?> coding_begins 09-13-2011, 07:30 PM for($i = 0; $i < 30; $i++) { $d[$i]=strtotime('today - ' . $i . ' day'); $d1[$i]=strtotime('today - ' . $i+1 . ' day'); echo "D:".date('Y m d', $d[$i])."<br>"; echo "D+1:".date('Y m d', $d1[$i])."<br>"; } It prints the same date again..it does not increment the value for D+1: D:2011 09 13 D+1:2011 09 14 D:2011 09 12 D+1:2011 09 14 D:2011 09 11 D+1:2011 09 14 D:2011 09 10 ... coding_begins 09-13-2011, 07:32 PM never mind..i missed the y variable.. coding_begins 09-13-2011, 07:34 PM But it prints the first value of D+1 as 1969-... for($i = 0; $i < 30; $i++) { $y=$i-1; $d[$i]=strtotime('today - ' . $i . ' day'); $d1[$i]=strtotime('today - ' . $y . ' day'); echo "D:".date('Y m d', $d[$i])."<br>"; echo "D+1:".date('Y m d', $d1[$i])."<br>"; } D:2011 09 13 D+1:1969 12 31 D:2011 09 12 D+1:2011 09 13 D:2011 09 11 Inigoesdr 09-13-2011, 08:10 PM Whoops. I misread what you asked for. Try this for the last 30 days: for($i = 0; $i < 30; $i++) echo date('Y m d', strtotime('today' . ($i > 0 ? ' - ' . $i . ' day' : ''))), '<br />'; // Or echo date('Y m d'), '<br />'; for($i = 1; $i < 30; $i++) echo date('Y m d', strtotime('today - ' . $i . ' day')), '<br />'; BluePanther 09-13-2011, 08:16 PM I would have done this differently, but I want your opinion on whether it's better/worse than what's already been posted - or just different. // timestamp for right now $now = time(); // a day in seconds $day = 60*60*24; // Start $i at $now timestamp, increment it by 1 day until it's 30 days (< 31) // For last 30 days, change += to -= for($i = $now; $i < ($now+($day*31)); $i += $day ){ echo date("d-m-Y",$i)."\n"; } Inigoesdr 09-13-2011, 08:22 PM That depends on your criteria for better or worse. Mine is shorter and easier to read, but yours might be faster(by a few ms, though you could optimize it some more). The output(other than the date format and newline) appears to be the same. BluePanther 09-13-2011, 08:25 PM My criteria was exactly on those comparisons, but I wasn't sure about the time. I didn't think there would be much in it, but I don't entirely know how the strtotime() actually parses the 'today' operand. Thanks for your input :) |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum