In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date['Y-m-d H:i:s', strtotime["{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"]];
What is the best way to do this?
John Conde
215k98 gold badges447 silver badges489 bronze badges
asked Aug 17, 2009 at 5:20
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime[]
this will account for daylight savings time and leap year.
$datetime = new DateTime['2013-01-29'];
$datetime->modify['+1 day'];
echo $datetime->format['Y-m-d H:i:s'];
// Available in PHP 5.3
$datetime = new DateTime['2013-01-29'];
$datetime->add[new DateInterval['P1D']];
echo $datetime->format['Y-m-d H:i:s'];
// Available in PHP 5.4
echo [new DateTime['2013-01-29']]->add[new DateInterval['P1D']]->format['Y-m-d H:i:s'];
// Available in PHP 5.5
$start = new DateTimeImmutable['2013-01-29'];
$datetime = $start->modify['+1 day'];
echo $datetime->format['Y-m-d H:i:s'];
Jasper
75.3k13 gold badges149 silver badges145 bronze badges
answered Jan 31, 2013 at 1:57
John CondeJohn Conde
215k98 gold badges447 silver badges489 bronze badges
3
If you want to do this in PHP:
// replace time[] with the time stamp you want to add one day to
$startDate = time[];
date['Y-m-d H:i:s', strtotime['+1 day', $startDate]];
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD[CURRENT_DATE, INTERVAL 1 DAY];
answered Aug 17, 2009 at 5:23
RaYellRaYell
68.5k20 gold badges125 silver badges150 bronze badges
3
The DateTime constructor takes a parameter string time
. $time
can be different things, it has to respect the
datetime format.
There are some valid values as examples :
'now'
[the default value]2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime['now +1 day']; //Tomorrow
$dt = new \DateTime['2016-01-01 +1 day']; //2016-01-02
AnthonyB
1,9621 gold badge20 silver badges28 bronze badges
answered Sep 15, 2016 at 10:20
DmitryDmitry
4595 silver badges4 bronze badges
1
- Use
strtotime
to convert the string to a time stamp - Add a day to it [eg: by adding 86400 seconds [24 * 60 * 60]]
eg:
$time = strtotime[$myInput];
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
answered Aug 17, 2009 at 5:27
nickfnickf
525k198 gold badges640 silver badges720 bronze badges
3
You can use
$now = new DateTime[];
$date = $now->modify['+1 day']->format['Y-m-d H:i:s'];
answered Aug 1, 2019 at 10:43
OmarOmar
86611 silver badges12 bronze badges
You can use as following.
$start_date = date['Y-m-d H:i:s'];
$end_date = date["Y-m-d 23:59:59", strtotime['+3 days', strtotime[$start_date]]];
You can also set days as constant and use like below.
if [!defined['ADD_DAYS']] define['ADD_DAYS','+3 days'];
$end_date = date["Y-m-d 23:59:59", strtotime[ADD_DAYS, strtotime[$start_date]]];
answered Oct 29, 2019 at 5:50
user3216114user3216114
1893 silver badges13 bronze badges
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-]
$date = new Zend_Date[];
$date->add['24:00:00', Zend_Date::TIMES];
print $date->get[];
answered Aug 17, 2009 at 5:39
PawkaPawka
2,5043 gold badges24 silver badges32 bronze badges
1
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date['d/m/y', $timestamp];
$newDate = date['d/m/y', strtotime['+2 day', $timestamp]];
Here '+2 days' to add any number of days.
answered Aug 25, 2019 at 17:02
NiroshanNiroshan
3113 silver badges4 bronze badges
There is a more concise and intuitive way to add days to php date. Don't get me wrong, those php expressions are great, but you always have to google how to treat them. I miss auto-completion facility for that.
Here is how I like to handle those cases:
[new Future[
new DateTimeFromISO8601String['2014-11-21T06:04:31.321987+00:00'],
new OneDay[]
]]
->value[];
For me, it's way more intuitive and autocompletion works out of the box. No need to google for the solution each time.
As a nice bonus, you don't have to worry about formatting the resulting value, it's already is ISO8601 format.
This is meringue library, there are more examples here.
answered May 16, 2020 at 16:30
Vadim SamokhinVadim Samokhin
3,3144 gold badges39 silver badges66 bronze badges
One liner !
echo [new \DateTime['2016-01-01 +1 day']]->format['Y-m-d H:i:s'];
answered Sep 19 at 15:41
Brain90Brain90
1,45318 silver badges21 bronze badges