PHP, Get tomorrows date from date

后端 未结 11 1808
不思量自难忘°
不思量自难忘° 2020-11-30 02:54

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 pos

相关标签:
11条回答
  • 2020-11-30 03:11

    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');
    
    0 讨论(0)
  • 2020-11-30 03:18

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

    0 讨论(0)
  • 2020-11-30 03:20
    /**
     * 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);
    }
    
    0 讨论(0)
  • 2020-11-30 03:23

    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

    0 讨论(0)
  • 2020-11-30 03:23

    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.

    0 讨论(0)
  • 2020-11-30 03:26

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

    0 讨论(0)
提交回复
热议问题