Adding 1 day to the DATETIME format value

In some situations, I want to add 1 day to the value of my formatted DATETIME variable:

$start_date = date('Ymd H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}")); 

What is the best way to do this?

+71
date php mysql datetime
Aug 17 '09 at 5:20
source share
7 answers

If you want to do this in PHP:

 // replace time() with the time stamp you want to add one day to $startDate = time(); date('Ymd H:i:s', strtotime('+1 day', $startDate)); 

If you want to add a date in MySQL:

 -- replace CURRENT_DATE with the date you want to add one day to SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY); 
+52
Aug 17 '09 at 5:23
source share

Here is another way to do this using DateTime , which was introduced in PHP 5.2. Unlike using strtotime() , this will take into account daylight saving time and a leap year.

 $datetime = new DateTime('2013-01-29'); $datetime->modify('+1 day'); echo $datetime->format('Ymd H:i:s'); // Available in PHP 5.3 $datetime = new DateTime('2013-01-29'); $datetime->add(new DateInterval('P1D')); echo $datetime->format('Ymd H:i:s'); // Available in PHP 5.4 echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Ymd H:i:s'); // Available in PHP 5.5 $start = new DateTimeImmutable('2013-01-29'); $datetime = $start->modify('+1 day'); echo $datetime->format('Ymd H:i:s'); 
+181
Jan 31 '13 at 1:57
source share

The DateTime constructor takes a string time parameter. $time can be different things, it must follow the date and time format .

Some valid values โ€‹โ€‹are given as examples:

  • 'now' (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 
+20
Sep 15 '16 at 10:20
source share
  • Use strtotime to convert a string to a timestamp.
  • Add a day to it (for example: adding 86400 seconds (24 * 60 * 60))

eg:

 $time = strtotime($myInput); $newTime = $time + 86400; 

If he adds only 1 day, then using strtotime again is likely to be redundant.

+9
Aug 17 '09 at 5:27
source share

I suggest starting with Zend_Date classes from the Zend Framework . I know this is a bit offtopic, but I will like it :-)

 $date = new Zend_Date(); $date->add('24:00:00', Zend_Date::TIMES); print $date->get(); 
+1
Aug 17 '09 at 5:39
source share

you can use

 $now = new DateTime(); $date = $now->modify('+1 day')->format('Ymd H:i:s'); 
0
Aug 01 '19 at 10:43 on
source share

Using server request time to add days. It works as expected.

08/25/19 => 09/27/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.

0
Aug 25 '19 at 17:02
source share



All Articles