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.
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.
❮ PHP Date/Time Reference
Example
Add 40 days to the 15th of March, 2013:
<?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Try it Yourself »
Definition and Usage
The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.
Syntax
date_add(object, interval)
Parameter Values
object | Required. Specifies a DateTime object returned by date_create() |
interval | Required. Specifies a DateInterval object |
Technical Details
Returns a DateTime object on success. FALSE on failure |
5.3+ |
❮ PHP Date/Time Reference
The PHP date() function is used to format a date and/or a time.
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
Syntax
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Get a Date
The 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:
- d - Represents the day of the month (01 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
- l (lowercase 'L') - Represents the day of the week
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
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
Try it Yourself »
PHP Tip - Automatic Copyright Year
Use the date() function to automatically update the copyright year on your website:
Get a Time
Here are some characters that are commonly used for times:
- H - 24-hour format of an hour (00 to 23)
- h - 12-hour format of an hour with leading zeros (01 to 12)
- i - Minutes with leading zeros (00 to 59)
- s - Seconds with leading zeros (00 to 59)
- a - Lowercase Ante meridiem and Post meridiem (am or pm)
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 Zone
If 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
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
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 mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax
mktime(hour, minute, second, month, day, year)
The example below creates a date and time with the date() function from a number of parameters in the mktime() function:
Example
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
Try it Yourself »
Create a Date From a String With strtotime()
The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax
The example below creates a date and time from the strtotime() function:
Example
<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
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("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>
Try it Yourself »
However, strtotime() is not perfect, so remember to check the strings you put in there.
More Date Examples
The example below outputs the dates for the next six Saturdays:
Example
<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate) . "<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
Try it Yourself »
The example below outputs the number of days until 4th of July:
Example
<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>
Try it Yourself »
Complete PHP Date Reference
For 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!