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?
const date = new Date('2022-02-21'); const dateCopy = new Date(date. getTime()); dateCopy. setDate(dateCopy. getDate() + 1); // 👇️ Tue Feb 22 2022 console.
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");
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.
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');
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With