[php] PHP, Get tomorrows date from date

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?

This question is related to php date datetime strtotime

The answer is


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; }

/**
 * 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);
}

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


<? php 

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

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

?>

Since you tagged this with , 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.


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.


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

Where 86400 is the # of seconds in a day.


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


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

or

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

Help Link: STRTOTIME()


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!


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to strtotime

PHP: How to check if a date is today, yesterday or tomorrow PHP Converting Integer to Date, reverse of strtotime date() method, "A non well formed numeric value encountered" does not want to format a date passed in $_POST PHP strtotime +1 month adding an extra month PHP, Get tomorrows date from date Adding three months to a date in PHP How to get previous month and year relative to today, using strtotime and date? Adding 30 minutes to time formatted as H:i in PHP $date + 1 year? adding 30 minutes to datetime php/mysql