-2

Possible Duplicate:
Simplest way to increment a date in PHP?

I am very new to php. I have the current date and I want to increment the date day by day. I don't have any end condition. I want to increment the date day by day for 4 years or so. There should not be a ending for that. I don't have any other data's like end date, start date. I want to increment the current date one by one using any loop. If anyone knows please help me.

Community
  • 1
  • 1
Nithin
  • 383
  • 3
  • 9
  • please use the search function before littering StackOverflow with superfluous duplicates. How to increment dates has been asked a hundred times before and we do not want any more of these questions. – Gordon Dec 01 '11 at 11:59

5 Answers5

4
$i = 1;
while (your condition here)
{
   echo date('Y-m-d', strtotime('+' . $i++ . ' day'));
}
matino
  • 17,199
  • 8
  • 49
  • 58
1

As you can see, there are many ways to add a day to the current date. The other answers are simpler and quite valid, but the object oriented way is to use DateTime::add.

<?php
$date = new DateTime();
$date->add(new DateInterval('P1D'));
echo $date->format('Y-m-d') . "\n";
?>

It may seem daunting at first glance, but the benefit I’ve found is that DateTime, DateInterval, and DateTimeZone make the quirkiness of timezones and intervals much easier to handle.

Herbert
  • 5,698
  • 2
  • 26
  • 34
0

A day is 86400 seconds, so you can simply take the current time() and do +86400.

echo 'Tomorrow it\'s '.date('d-m-Y', time()+86400);
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
0

You can use the function strtotime for that, like this:

for ($i= 1; $i < 10; $i++) {
    $date = strtotime("+$i day", strtotime("2011-12-01"));
    echo date("Y-m-d", $date);
}
Rafael Steil
  • 4,538
  • 4
  • 25
  • 30
0

Try mktime(). Refer below example for Current Date after 4 years.

   echo date('m-d-Y',  mktime(0, 0, 0, date('m'), date('d'), date('Y')+4));

more details about mktime() is here http://php.net/manual/en/function.mktime.php

pinaldesai
  • 1,835
  • 2
  • 13
  • 22