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. Show 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:
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. strtotime( $EnglishDateTime, $time_now ) Parameters: This function accepts two optional parameters as mentioned above and described below.
Program: PHP program to add days to $Date in PHP using strtotime() function. php
Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. date_add(object, interval); Parameters: This function accepts
two parameters as mentioned above and described below:
Program: PHP program to add days to $Date in PHP using date_add() function. php
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 DescriptionObject-oriented style public
DateTime::add(DateInterval Procedural style date_add(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. Parametersobject Procedural style only: A DateTime object returned by date_create(). The function modifies this object. interval A DateInterval object Return Values Returns the modified
DateTime object for method chaining or See Also
+add a note User Contributed NotesThere are no user contributed notes for this page. How to add one day in PHP date?Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. Syntax: date_add(object, interval);
How to add day to Time in PHP?The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.
How to add dates in PHP?PHP date_add() Function
$date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d");
|