Jump to content

Dates 10 days in advance


pauldonnelly23

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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";
?>

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

 

 

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.