Increase days to php current Date()

前端 未结 11 1183
予麋鹿
予麋鹿 2020-12-04 19:13

How do I add a certain number of days to the current date in PHP?

I already got the current date with:

$today = date(\'y:m:d\');

Ju

相关标签:
11条回答
  • 2020-12-04 19:30

    The simplest way to add x no. of days..

    echo date('Y-m-d',strtotime('+1 day'));    //+1 day from today
    

    OR from specified date...

    echo date('Y-m-d',strtotime('+1 day', strtotime('2007-02-28')));
    
    0 讨论(0)
  • 2020-12-04 19:33

    a day is 86400 seconds.

    $tomorrow = date('y:m:d', time() + 86400);
    
    0 讨论(0)
  • 2020-12-04 19:35

    If you need this code in several places then I'd suggest that you add a short function to keep your code simpler and easier to test.

    function add_days( $days, $from_date = null ) {
        if ( is_numeric( $from_date ) ) { 
            $new_date = $from_date; 
        } else { 
            $new_date = time();
        }
    
        // Timestamp is the number of seconds since an event in the past
        // To increate the value by one day we have to add 86400 seconds to the value
        // 86400 = 24h * 60m * 60s
        $new_date += $days * 86400;
    
        return $new_date;
    }
    

    Then you can use it anywhere like this:

    $today       = add_days( 0 );
    $tomorrow    = add_days( 1 );
    $yesterday   = add_days( -1 );
    $in_36_hours = add_days( 1.5 );
    
    $first_reminder  = add_days( 10 );
    $second_reminder = add_days( 5, $first_reminder );
    $last_reminder   = add_days( 3, $second_reminder );
    
    0 讨论(0)
  • 2020-12-04 19:37

    With php 5.3

        $date = new DateTime();
        $interval = new DateInterval('P1D');
        echo $date->format('Y-m-d') , PHP_EOL;
        $date->add($interval);
        echo $date->format('Y-m-d'), PHP_EOL;
        $date->add($interval);
        echo $date->format('Y-m-d'), PHP_EOL;
    

    will output

    2012-12-24

    2012-12-25

    2012-12-26

    0 讨论(0)
  • 2020-12-04 19:42

    php supports c style date functions. You can add or substract date-periods with English-language style phrases via the strtotime function. examples...

    $Today=date('y:m:d');
    
    // add 3 days to date
    $NewDate=Date('y:m:d', strtotime('+3 days'));
    
    // subtract 3 days from date
    $NewDate=Date('y:m:d', strtotime('-3 days'));
    
    // PHP returns last sunday's date
    $NewDate=Date('y:m:d', strtotime('Last Sunday'));
    
    // One week from last sunday
    $NewDate=Date('y:m:d', strtotime('+7 days Last Sunday'));
    

    or

    <select id="date_list" class="form-control" style="width:100%;">
    <?php
    $max_dates = 15;
    $countDates = 0;
    while ($countDates < $max_dates) {
        $NewDate=Date('F d, Y', strtotime("+".$countDates." days"));
        echo "<option>" . $NewDate . "</option>";
        $countDates += 1;
    }
    ?>
    

    0 讨论(0)
  • 2020-12-04 19:42
    $NewDate=Date('Y-m-d', strtotime('+365 days'));
    

    echo $NewDate; //2020-05-21

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