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. 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. ❮ PHP Date/Time Reference ExampleAdd 40 days to the 15th of March, 2013: <?php Try it Yourself » Definition and UsageThe date_add() function adds some days, months, years, hours, minutes, and seconds to a date. Syntaxdate_add(object, interval) Parameter Values
Technical Details
❮ PHP Date/Time Reference The PHP The PHP Date() FunctionThe PHP Syntax
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. Get a DateThe required format parameter of the date() function specifies how to format the date (or time). Here are some characters that are commonly used for dates:
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting. The example below formats today's date in three different ways: Example<?php Try it Yourself » PHP Tip - Automatic Copyright YearUse the Get a TimeHere are some characters that are commonly used for times:
The example below outputs the current time in the specified format: Note that the PHP date() function will return the current date/time of the server! Get Your Time ZoneIf the time you got back from the code is not correct, it's probably because your server is in another country or set up for a different timezone. So, if you need the time to be correct according to a specific location, you can set the timezone you want to use. The example below sets the timezone to "America/New_York", then outputs the current time in the specified format: Example<?php Try it Yourself » Create a Date With mktime()The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP Syntaxmktime(hour, minute, second, month, day, year) The example below creates a date and time with the Example<?php Try it Yourself » Create a Date From a String With strtotime()The PHP SyntaxThe example below creates a date and time from the Example<?php Try it Yourself » PHP is quite clever about converting a string to a date, so you can put in various values: Example<?php $d=strtotime("next Saturday"); $d=strtotime("+3 Months"); Try it Yourself » However, More Date ExamplesThe example below outputs the dates for the next six Saturdays: Example<?php while ($startdate < $enddate) { Try it Yourself » The example below outputs the number of days until 4th of July: Example<?php Try it Yourself » Complete PHP Date ReferenceFor a complete reference of all date functions, go to our complete PHP Date Reference. The reference contains a brief description, and examples of use, for each function! PHP ExercisesHow can add days in date 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");
How do I +1 a date in PHP?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.
What does date () do in PHP?The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.
|