PHP, Get tomorrows date from date

PhpDateDatetimeStrtotime

Php Problem Overview


I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.

How is this possible with PHP?

Php Solutions


Solution 1 - Php

Use DateTime

$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');

Or:

$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');

Or:

$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');

Or in PHP 5.4+:

echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
                                 ->format('Y-m-d H:i:s');

Solution 2 - Php

 $tomorrow = date("Y-m-d", strtotime('tomorrow'));

or

  $tomorrow = date("Y-m-d", strtotime("+1 day"));

Help Link: STRTOTIME()

Solution 3 - Php

Since you tagged this with [tag:strtotime], you can use it with the +1 day modifier like so:

$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));

That said, it's a much better solution to use DateTime.

Solution 4 - Php

<? php 

//1 Day = 24*60*60 = 86400

echo date("d-m-Y", time()+86400); 

?>

Solution 5 - Php

echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));

Solution 6 - Php

Use DateTime:

To get tomorrow from now :

$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

>Results : 28/06/2017 08.13.20

To get tomorrow from a date :

$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

>Results : 11/06/2017 08.16.35

Hope it helps!

Solution 7 - Php

/**
 * get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
 *
 * @param string
 *
 * @return string
 */
public static function getTomorrowsDate($format = 'Y-m-d')
{
    $date = new DateTime();
    $date->add(DateInterval::createFromDateString('tomorrow'));

    return $date->format($format);
}

Solution 8 - Php

By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );

echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );

Should do it

Solution 9 - Php

First, coming up with correct abstractions is always a key. key to readability, maintainability, and extendability.

Here, quite obvious candidate is an ISO8601DateTime. There are at least two implementations: first one is a parsed datetime from a string, and the second one is tomorrow. Hence, there are two classes that can be used, and their combination results in (almost) desired outcome:

new Tomorrow(new FromISO8601('2013-01-22'));

Both objects are an ISO8601 datetime, so their textual representation is not exactly what you need. So the final stroke is to make them take a date-form:

new Date(
    new Tomorrow(
        new FromISO8601('2013-01-22')
    )
);

Since you need a textual representation, not just an object, you invoke a value() method.

For more about this approach, take a look at this post.

Solution 10 - Php

here's working function

function plus_one_day($date){
 $date2 = formatDate4db($date);
 $date1 = str_replace('-', '/', $date2);
 $tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
 return $tomorrow; }

Solution 11 - Php

$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);

Where 86400 is the # of seconds in a day.

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
QuestionJustinView Question on Stackoverflow
Solution 1 - PhpJohn CondeView Answer on Stackoverflow
Solution 2 - PhpLaura CheschesView Answer on Stackoverflow
Solution 3 - PhpRudi VisserView Answer on Stackoverflow
Solution 4 - PhpandyView Answer on Stackoverflow
Solution 5 - PhpABDUL JAMALView Answer on Stackoverflow
Solution 6 - PhpGregorioView Answer on Stackoverflow
Solution 7 - PhpcrmpiccoView Answer on Stackoverflow
Solution 8 - PhpDiogo Neves - MangaruView Answer on Stackoverflow
Solution 9 - PhpVadim SamokhinView Answer on Stackoverflow
Solution 10 - PhpMuhammad Awais ZulifqarView Answer on Stackoverflow
Solution 11 - PhpJasonView Answer on Stackoverflow