pauldonnelly23 Posted February 15, 2010 Share Posted February 15, 2010 Hi, Im hoping someone can help. I am trying to get the dates from the current day and 10 days in advance. Example: Monday 15th Feb 2010 Tuesday 16th Feb 2010 Wednesday 17th Feb 2010 Thursday 18th Feb 2010....And so on up to 10 days. As each day pasts it would get rid of the previous day from the list. I hope you understand what I am trying to do and I hope you can help. Kind Regards, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/ Share on other sites More sharing options...
salathe Posted February 15, 2010 Share Posted February 15, 2010 Are you using PHP 5.3? If so: $today = new DateTime; $interval = new DateInterval('P1D'); $period = new DatePeriod($today, $interval, 10); foreach ($period as $datetime) { echo $datetime->format('l jS M Y') . PHP_EOL; } If you're not so lucky as to to be able to use PHP 5.3 right now, do let us know what is available for you. Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1012868 Share on other sites More sharing options...
pauldonnelly23 Posted February 15, 2010 Author Share Posted February 15, 2010 Thanks for your prompt reply Salathe. My hosting is setup as PHP5 (Running as an Apache Module). Unfortunately the code below is not showing anything. <?php $today = new DateTime; $interval = new DateInterval('P1D'); $period = new DatePeriod($today, $interval, 10); foreach ($period as $datetime) { echo $datetime->format('l jS M Y') . PHP_EOL; } ?> Can you help with this? Many Thanks, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1012870 Share on other sites More sharing options...
harristweed Posted February 15, 2010 Share Posted February 15, 2010 <?php $date_today=date("l jS M Y"); echo $date_today."<br />\n"; $day_1 = mktime(0, 0, 0, date("m"), date("d")+1, date("Y")); $day1=date("l jS M Y",$day_1); echo $day1."<br />\n"; $day_2 = mktime(0, 0, 0, date("m"), date("d")+2, date("Y")); $day2=date("l jS M Y",$day_2); echo $day2."<br />\n"; echo"....<br />\n"; $day_9 = mktime(0, 0, 0, date("m"), date("d")+9, date("Y")); $day9=date("l jS M Y",$day_9); echo $day9."<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1012875 Share on other sites More sharing options...
salathe Posted February 15, 2010 Share Posted February 15, 2010 Unfortunately the code below is not showing anything. In that case, the chances are that you're running some version of PHP 5.2 so the DatePeriod/Interval classes will not be available. The fact that nothing at all is shown means that you should probably turn on error reporting (and displaying; search the forums if you're not sure how to do that) since if you do that, PHP will kindly tell you that those classes are not available. Can you help with this? Of course. harristweed's code above should work for you, or you can use something like the code below which uses a for loop to save typing the same thing 10 times, strtotime to move the date forward by some days and date to print a nice representation of each date. for ($i = 0; $i <= 10; $i++) { $date = strtotime("+$i days"); echo date("l jS M Y", $date) . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1012884 Share on other sites More sharing options...
pauldonnelly23 Posted February 16, 2010 Author Share Posted February 16, 2010 Salathe, thanks for the explanation it was really helpful. Also that snippet of code is exactly what I needed, thanks again. Kind regards, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1013024 Share on other sites More sharing options...
pauldonnelly23 Posted February 16, 2010 Author Share Posted February 16, 2010 salathe, on another note can you explain what PHP_EOL does? Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1013026 Share on other sites More sharing options...
salathe Posted February 16, 2010 Share Posted February 16, 2010 PHP_EOL is a constant which contains the line-ending character(s) suitable for your server. For example, for a server running on windows it contains the carriage return and newline characters (\r\n) while for a linux box it will contain just the newline character (\n). See some other constants. It added nothing critical to the example code other than placing a line break between each of the dates. Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1013100 Share on other sites More sharing options...
pauldonnelly23 Posted February 16, 2010 Author Share Posted February 16, 2010 I see, thats great to know. Thanks for that and for your help Salathe. Regards, Paul Quote Link to comment https://forums.phpfreaks.com/topic/192193-dates-10-days-in-advance/#findComment-1013101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.