Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding 1 day to a DATETIME format value

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?

like image 650
ian Avatar asked Aug 17 '09 05:08

ian


People also ask

How do I add 1 day to a date?

const date = new Date('2022-02-21'); const dateCopy = new Date(date. getTime()); dateCopy. setDate(dateCopy. getDate() + 1); // 👇️ Tue Feb 22 2022 console.

How to add 1 date in php?

PHP date_add() Function $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d");

How to add DateTime in php?

PHP | DateTime add() Function The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object.


2 Answers

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'); 
like image 134
John Conde Avatar answered Sep 25 '22 03:09

John Conde


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); 
like image 28
RaYell Avatar answered Sep 21 '22 03:09

RaYell