230

My code to add one day to a date returns a date before day adding: 2009-09-30 20:24:00 date after adding one day SHOULD be rolled over to the next month: 1970-01-01 17:33:29

<?php

    //add day to date test for month roll over

    $stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00"));

    echo 'date before day adding: '.$stop_date; 

    $stop_date = date('Y-m-d H:i:s', strtotime('+1 day', $stop_date));

    echo ' date after adding one day. SHOULD be rolled over to the next month: '.$stop_date;
?>

I have used pretty similar code before, what am I doing wrong here?

Shoaib Quraishi
  • 232
  • 2
  • 17
ian
  • 11,605
  • 25
  • 69
  • 96

13 Answers13

414
<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date; 
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>

For PHP 5.2.0+, you may also do as follows:

$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s'); 
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');
w35l3y
  • 8,613
  • 3
  • 39
  • 51
  • 3
    Thanks. Solved it as: $stop_date = date('Y-m-d H:i:s', strtotime( "$stop_date + 1 day" )); – ian Sep 08 '09 at 16:07
  • 13
    You should not use a variable in a string. You should use:`date('Y-m-d H:i:s', strtotime($stop_date . ' + 1 day'));` as in the answer that @w35l3y gave you. – Cas Bloem Feb 12 '14 at 09:52
  • You should add a call to date_default_timezone_set function before running this code. For example add date_default_timezone_set('Europe/Rome'); – Luca Mastrostefano Jul 09 '17 at 11:26
  • Error: Call to a member function modify() on a non-object in /public_html/untitled5.php on line 72 – hamish Jul 23 '21 at 03:28
  • You did something wrong, @hamish - https://www.php.net/manual/en/datetime.modify.php#example-2002 – w35l3y Jul 28 '21 at 20:23
  • PHP Fatal error: Uncaught Error: Call to undefined method DateTime::modifiy(). By the way, using "/usr/local/php74/bin/php" to execute php file. It just doesn't work. – Gabe Hiemstra Jul 21 '22 at 07:43
  • it is "modify", not "modifiy" @GabeHiemstra – w35l3y Jul 26 '22 at 21:35
134
$date = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
85

It Worked for me: For Current Date

$date = date('Y-m-d', strtotime("+1 day"));

for anydate:

date('Y-m-d', strtotime("+1 day", strtotime($date)));
Jay Momaya
  • 1,831
  • 19
  • 32
33

Simplest solution:

$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');
minlare
  • 2,454
  • 25
  • 46
  • 1
    I like your way in a one liner `echo (new DateTime('+1 day'))->format('Y-m-d H:i:s');` – Aba Jul 01 '16 at 14:28
  • @Aba I don't think your one liner works in older versions of PHP because I tried it and I got an error: `Unexpected T_OBJECT_OPERATOR” error in PHP` – Zachary Weixelbaum Sep 01 '17 at 12:20
9

Simple to read and understand way:

$original_date = "2009-09-29";

$time_original = strtotime($original_date);
$time_add      = $time_original + (3600*24); //add seconds of one day

$new_date      = date("Y-m-d", $time_add);

echo $new_date;
joan16v
  • 5,055
  • 4
  • 49
  • 49
7

Try this

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));
SeanWM
  • 16,789
  • 7
  • 51
  • 83
user1987095
  • 439
  • 2
  • 8
  • 18
7

The modify() method that can be used to add increments to an existing DateTime value.

Create a new DateTime object with the current date and time:

$due_dt = new DateTime();

Once you have the DateTime object, you can manipulate its value by adding or subtracting time periods:

$due_dt->modify('+1 day');

You can read more on the PHP Manual.

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
Kal
  • 71
  • 1
  • 1
4

I always just add 86400 (seconds in a day):

$stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00") + 86400);

echo 'date after adding 1 day: '.$stop_date; 

It's not the slickest way you could probably do it, but it works!

Doug Hays
  • 1,507
  • 11
  • 13
  • How do you deal with leap seconds when adding 86400 won't work as there's 86401 seconds in that day? (ok, I know it only happens every few years, but depending on the app this might be important) – Glen Sep 08 '09 at 16:05
  • 1
    Not all days have 86400 seconds in them. In fact, in most places in the US there are 3600 fewer or additional seconds twice a year. – Peter Kovacs Sep 08 '09 at 16:10
  • 2
    You can safely ignore leap seconds, since "Unix time" does. This is somewhat complicated, but read this article for more info: http://derickrethans.nl/leap_seconds_and_what_to_do_with_them.php – Christian Davén Sep 08 '09 at 17:47
  • 2
    You should avoid this solution. Here is why: http://stackoverflow.com/questions/2613338/date-returning-wrong-day-although-the-timestamp-is-correct – mspir Sep 21 '12 at 20:23
  • 1
    Do not add date by adding 86400 seconds to previous day! You will have bug in your code! This does not work on Daylight Saving days! – sbrbot Oct 26 '12 at 09:05
1

While I agree with Doug Hays' answer, I'll chime in here to say that the reason your code doesn't work is because strtotime() expects an INT as the 2nd argument, not a string (even one that represents a date)

If you turn on max error reporting you'll see this as a "A non well formed numeric value" error which is E_NOTICE level.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
1
<?php

function plusTimetoOldtime($Old_Time,$getFormat,$Plus_Time) {
    return date($getFormat,strtotime(date($getFormat,$Old_Time).$Plus_Time));
}

$Old_Time = strtotime("now");
$Plus_Time = '+1 day';
$getFormat = 'Y-m-d H:i:s';

echo plusTimetoOldtime($Old_Time,$getFormat,$Plus_Time);

?>
Smar ts
  • 49
  • 5
0

The following code get the first day of January of current year (but it can be a another date) and add 365 days to that day (but it can be N number of days) using DateTime class and its method modify() and format():

echo (new DateTime((new DateTime())->modify('first day of January this year')->format('Y-m-d')))->modify('+365 days')->format('Y-m-d');
Carlos Espinoza
  • 1,115
  • 11
  • 13
0

As we often receive ISO strings via an API from another time zone, we can make the conversion to local time on the fly:

// The machine local time is GMT+2, but we received a date at GMT+0 (UTC)
echo date(DATE_ISO8601, strtotime("-1 hour", strtotime("2022-08-17T23:25:51-00:00")));
// 2022-08-18T00:25:51+0200
NVRM
  • 11,480
  • 1
  • 88
  • 87
-1

You can add one day to today's date using DateTime() and DateInterval() like this:

echo (new DateTime())->add(new DateInterval('P1D'))->format('Y-m-d');

OR a specific date:

$date = '2009-09-30 20:24:00';
echo (new DateTime($date))->add(new DateInterval('P1D'))->format('Y-m-d');