Simplest way to increment a date in PHP?

93,852

Solution 1

A clean way is to use strtotime()

$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);

Will give you the 2007-03-01

Solution 2

It's cleaner and simpler to add 86400. :)

The high-tech way is to do:

$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');

but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.

Solution 3

You can do the addition right inside strtotime, e.g.

 $today="2007-02-28";
 $nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));

Solution 4

Another way is to use function mktime(). It is very useful function...

$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));

but I think strtotime() is better in that situation...

Solution 5

The simplest way...

echo date('Y-m-d',strtotime("+1 day"));    //from today

OR from specified date...

echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
Share:
93,852
davr
Author by

davr

Hello World

Updated on July 09, 2022

Comments

  • davr
    davr almost 2 years

    Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.

  • davr
    davr about 15 years
    Stupid CentOS only has PHP 5.1, and DateTime is introduced in 5.2. I guess I finally have to upgrade to an unofficial centos php package then.
  • chaos
    chaos about 15 years
    Oh, neat. Didn't know that. It's almost like good old Date::Manip.
  • Deva
    Deva about 15 years
    Er, no. Just adding 86400 is false simplicity.
  • chaos
    chaos about 15 years
    Could you explain what you mean by that?
  • Tom Haigh
    Tom Haigh about 15 years
    It is quite easy to understand what 24*60*60 means at a glance. 86400 less so.
  • Tom Haigh
    Tom Haigh about 15 years
    nice, I didn't realise that mktime() would increment the month like that when you go over
  • chaos
    chaos about 15 years
    Ah. I guess I've been doing this too long, then. 86400 is as recognizable to me as 65536.
  • Deva
    Deva about 15 years
    @chaos: If your smallest unit of working time is a day, then you can usually get away with adding or substracting units of 86400. If it is smaller, you need to be aware of daylight savings and time zones. In which case, use the built-in functions. They will get it right more often than you will.
  • Jimmy T.
    Jimmy T. over 7 years
    Not all days are 86400 seconds long (leap seconds).
  • Eatsleeprace Livelife
    Eatsleeprace Livelife over 6 years
    /("1month")= will increment by month. //("1day")= will increment by day. //(date("Y-m-d"))=start to increment date on you current date.
  • astidham2003
    astidham2003 over 6 years
    You should edit your answer to include the explanation of the code, instead of having the explanation in a comment.