216

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
  • 202
  • 2
  • 16
ian
  • 10,995
  • 25
  • 67
  • 96

12 Answers12

392
<?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,086
  • 3
  • 37
  • 50
  • 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
129
$date = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";
Prasanth Bendra
  • 29,105
  • 8
  • 50
  • 70
68

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,548
  • 16
  • 27
30

Simplest solution:

$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');
minlare
  • 2,098
  • 21
  • 44
  • 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
8

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
  • 4,863
  • 2
  • 47
  • 48
7

Try this

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));
SeanWM
  • 16,319
  • 7
  • 49
  • 83
user1987095
  • 429
  • 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
  • 6,303
  • 4
  • 39
  • 52
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,517
  • 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
  • 103,526
  • 30
  • 178
  • 200
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
  • 21
  • 4
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,057
  • 11
  • 13
-1

Since you already have an answer to what's wrong with your code, I can bring another perspective on how you can play with datetimes generally, and solve your problem specifically.

Oftentimes you find yourself posing a problem in terms of solution. This is just one of the reasons you end up with an imperative code. It's great if it works though; there are just other, arguably more maintainable alternatives. One of them is a declarative code. The point is asking what you need, instead of how to get there.

In your particular case, this can look like the following. First, you need to find out what is it that you're looking for, that is, discover abstractions. In your case, it looks like you need a date. Not just any date, but the one having some standard representation. Say, ISO8601 date. There are at least two implementations: the first one is a date parsed from an ISO8601-formatted string (or a string in any other format actually), and the second is some future date which is a day later. Thus, the whole code could look like that:

(new Future(
    new DateTimeParsedFromISO8601('2009-09-30 20:24:00'),
    new OneDay()
))
    ->value();

For more examples with datetime juggling check out this one.

Vadim Samokhin
  • 3,274
  • 4
  • 39
  • 66