Simplest way to increment a date in PHP?
Asked Answered
E

9

43

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.

English answered 18/3, 2009 at 23:34 Comment(0)
L
87

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

Lagos answered 18/3, 2009 at 23:41 Comment(0)
T
36

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.

Timetable answered 18/3, 2009 at 23:35 Comment(7)
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.English
Er, no. Just adding 86400 is false simplicity.Monochromatism
Could you explain what you mean by that?Timetable
It is quite easy to understand what 24*60*60 means at a glance. 86400 less so.Elliot
Ah. I guess I've been doing this too long, then. 86400 is as recognizable to me as 65536.Timetable
@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.Monochromatism
Not all days are 86400 seconds long (leap seconds).Wembley
C
17

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

 $today="2007-02-28";
 $nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
Copyhold answered 18/3, 2009 at 23:42 Comment(1)
Oh, neat. Didn't know that. It's almost like good old Date::Manip.Timetable
T
6

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...

Thaumaturgy answered 19/3, 2009 at 0:48 Comment(1)
nice, I didn't realise that mktime() would increment the month like that when you go overElliot
B
6

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')));
Bookseller answered 18/1, 2015 at 15:20 Comment(0)
V
0

Hello you can try this below especially if you are french

$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));

to translate in french you can use the setlocale() function or the function below :

function fr_date($date){

    $date = explode(' ', $date);

    $date = str_replace('Monday','Lundi',$date);
    $date = str_replace('Tuesday','Mardi',$date);
    $date = str_replace('Wednesday','Mercredi',$date);
    $date = str_replace('Thursday','Jeudi',$date);
    $date = str_replace('Friday','Vendredi',$date);
    $date = str_replace('Saturday','Samedi',$date);
    $date = str_replace('Sunday','Dimanche',$date);

    $date = str_replace('January','Janvier',$date);
    $date = str_replace('February','Février',$date);
    $date = str_replace('March','Mars',$date);
    $date = str_replace('April','Avril',$date);
    $date = str_replace('May','Mai',$date);
    $date = str_replace('June','Juin',$date);
    $date = str_replace('July','Juillet',$date);
    $date = str_replace('August','Août',$date);
    $date = str_replace('September','Septembre',$date);
    $date = str_replace('October','Octobre',$date);
    $date = str_replace('November','Novembre',$date);
    $date = str_replace('December','Décembre',$date);

    $date = implode(' ',$date);
    return $date;

}
Vinnievinnitsa answered 18/7, 2017 at 21:11 Comment(0)
K
0
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
 $new_date = date("Y-m-d", $your_date++);
Kenyakenyatta answered 20/10, 2017 at 1:3 Comment(2)
/("1month")= will increment by month. //("1day")= will increment by day. //(date("Y-m-d"))=start to increment date on you current date.Kenyakenyatta
You should edit your answer to include the explanation of the code, instead of having the explanation in a comment.Pikeperch
C
0

use strtotime() With date Formate

echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
Caravaggio answered 18/3, 2021 at 10:56 Comment(0)
K
-1
 $early_start_date = date2sql($_POST['early_leave_date']);


        $date = new DateTime($early_start_date);
        $date->modify('+1 day');


        $date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
        $date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);

        $interval = date_diff($date_a, $date_b);
Kantianism answered 4/9, 2016 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.