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?
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.
Simplest solution:
$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');
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.
$date = new DateTime('2000-12-31');
$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";
Try this
echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));
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;
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');
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.
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)));
<?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);
?>
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!
Source: Stackoverflow.com