Adding one day to a date

PhpDateDatetime

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

<?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');

Solution 2 - Php

$date = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";

Solution 3 - Php

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)));

Solution 4 - Php

Simplest solution:

$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');

Solution 5 - Php

Try this

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));

Solution 6 - Php

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;

Solution 7 - Php

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.

Solution 8 - Php

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!

Solution 9 - Php

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.

Solution 10 - Php

<?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);

?>

Solution 11 - Php

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');

Solution 12 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionianView Question on Stackoverflow
Solution 1 - Phpw35l3yView Answer on Stackoverflow
Solution 2 - PhpPrasanth BendraView Answer on Stackoverflow
Solution 3 - PhpJay MomayaView Answer on Stackoverflow
Solution 4 - PhpminlareView Answer on Stackoverflow
Solution 5 - Phpuser1987095View Answer on Stackoverflow
Solution 6 - Phpjoan16vView Answer on Stackoverflow
Solution 7 - PhpKalView Answer on Stackoverflow
Solution 8 - PhpDoug HaysView Answer on Stackoverflow
Solution 9 - PhpPeter BaileyView Answer on Stackoverflow
Solution 10 - PhpSmar tsView Answer on Stackoverflow
Solution 11 - PhpCarlos EspinozaView Answer on Stackoverflow
Solution 12 - PhpVadim SamokhinView Answer on Stackoverflow