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.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime(), date_add() in
PHP.
Method 1: Using strtotime() Function: The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp.
Syntax:
Parameters: This function accepts two optional parameters as mentioned above and described below.
- $EnglishDateTime: This parameter specifies the English textual date-time description, which represents the date or time to be returned.
- $time_now: This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.
Program: PHP program to add days to $Date in PHP using strtotime() function.
php
<?php
$Date = "2019-05-10";
echo date('Y-m-d', strtotime($Date. ' + 10 days'));
?>
Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds.
Syntax:
Parameters: This function accepts
two parameters as mentioned above and described below:
- Object: It specifies the DateTime object returned by date_create() function.
- Interval: It specifies the DateInterval object.
Program: PHP program to add days to $Date in PHP using date_add() function.
php
<?php
$date = date_create("2019-05-10");
date_add($date, date_interval_create_from_date_string("10 days"));
echo date_format($date, "Y-m-d");
?>
Submit a Pull Request Report a Bug
date_add
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
DateTime::add -- date_add — Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
Description
Object-oriented style
public DateTime::add(DateInterval $interval): DateTime
Procedural style
date_add(DateTime $object, DateInterval $interval): DateTime
Adds the specified DateInterval object to the specified DateTime object.
Like DateTimeImmutable::add() but works with DateTime.
The procedural version takes the DateTime object as its first argument.
Parameters
objectProcedural style only: A DateTime object returned by date_create(). The function modifies this object.
intervalA DateInterval object
Return Values
Returns the modified DateTime object for method chaining or false on failure.
See Also
- DateTimeImmutable::add() - Returns a new object, with added amount of days, months, years, hours, minutes and seconds
+add a note
User Contributed Notes
There are no user contributed notes for this page.