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)));
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();
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');
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
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.
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.
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
strtotime
to convert the string to a time stampeg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
Source: Stackoverflow.com