110

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
  • 217,595
  • 99
  • 455
  • 496
ian
  • 11,605
  • 25
  • 69
  • 96

9 Answers9

255

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,717
  • 14
  • 151
  • 146
John Conde
  • 217,595
  • 99
  • 455
  • 496
77

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);
RaYell
  • 69,610
  • 20
  • 126
  • 152
  • wont this give me the current time? i still need to keep my user inputted time. – ian Aug 17 '09 at 05:26
  • Yes it will but I marked you what values you should change to the custom time. You can just replace `time()` with `strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}")`. – RaYell Aug 17 '09 at 05:37
  • this is not summer-/wintertime save! Please use DST save date/time manipulation – steven Jul 28 '17 at 10:35
37

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
Anthony
  • 2,014
  • 2
  • 19
  • 29
Dmitry
  • 489
  • 5
  • 4
  • 3
    Answering without explanation? Read [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – Shashanth Sep 15 '16 at 10:26
13
  1. Use strtotime to convert the string to a time stamp
  2. 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.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • So I need to convert it to a TIMESTAMP then back to a DATETIME format? – ian Aug 17 '09 at 05:32
  • 4
    I guess it should also be noted that not every day is 24 hours long. In most places in the world, daylight savings (summer time) means that one day a year is only 23 hours, and one other day is 25 hours. – nickf Jan 16 '12 at 10:21
  • 1
    Ups. You convert a datetime to a timestamp, so it's UTC based. And _UTC_ days are always 24h long. When converting back with `date()` the local timezone is taken into account which will take care of daylight saving. – rabudde Oct 25 '18 at 07:58
13

You can use

$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
Omar
  • 901
  • 11
  • 14
7

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)));
user3216114
  • 305
  • 4
  • 13
1

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();
Pawka
  • 2,526
  • 3
  • 25
  • 32
1

One liner !

echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');
Brain90
  • 1,551
  • 18
  • 21
0

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.

Niroshan
  • 360
  • 3
  • 5