PHP - add 1 day to date format mm-dd-yyyy

PhpDate

Php Problem Overview


<?php
    $date = "04-15-2013";
    $date = strtotime($date);
    $date = strtotime("+1 day", $date);
    echo date('m-d-Y', $date);
?>

This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.

The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.

Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y? Would I be better off learning how to use DateTime?

Php Solutions


Solution 1 - Php

there you go

$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));

echo $tomorrow;

this will output

04-16-2013

Documentation for both function
date
strtotime

Solution 2 - Php

$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');

See it in action

Or in PHP 5.4+

echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');

reference

Solution 3 - Php

$date = strtotime("+1 day");
echo date('m-d-y',$date);

Solution 4 - Php

use http://www.php.net/manual/en/datetime.add.php like

$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');

output

2000-01-2

Solution 5 - Php

The format you've used is not recognized by strtotime(). Replace

$date = "04-15-2013";

by

$date = "04/15/2013";

Or if you want to use - then use the following line with the year in front:

$date = "2013-04-15";

Solution 6 - Php

Actually I wanted same alike thing, To get one year backward date, for a given date! :-)

With the hint of above answer from @mohammad mohsenipur I got to the following link, via his given link!

Luckily, there is a method same as date_add method, named date_sub method! :-) I do the following to get done what I wanted!

$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');

Hopes this answer will help somebody too! :-)

Good luck guys!

Solution 7 - Php

Try with below code:

<?php
   $date = "2022-01-05";
   $to_date = date('Y-m-d', strtotime($date.' +1 day')); 
   echo $to_date;
?>

Output:

2022-01-06

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayrView Question on Stackoverflow
Solution 1 - PhpFabioView Answer on Stackoverflow
Solution 2 - PhpJohn CondeView Answer on Stackoverflow
Solution 3 - PhpinsoftserviceView Answer on Stackoverflow
Solution 4 - Phpmohammad mohsenipurView Answer on Stackoverflow
Solution 5 - Phphek2mglView Answer on Stackoverflow
Solution 6 - PhpRandika VishmanView Answer on Stackoverflow
Solution 7 - PhpstalinrajindianView Answer on Stackoverflow