Increase days to php current Date()

前端 未结 11 1184
予麋鹿
予麋鹿 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:43

    You can also use Object Oriented Programming (OOP) instead of procedural programming:

    $fiveDays = new DateInterval('P5D');
    $today = new DateTime();
    $fiveDaysAgo = $today->sub(fiveDays); // or ->add(fiveDays); to add 5 days
    

    Or with just one line of code:

    $fiveDaysAgo = (new DateTime())->sub(new DateInterval('P5D'));
    
    0 讨论(0)
  • 2020-12-04 19:48

    Add 15 day to a select element (using "Alive to Die" suggestion)

    <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:52

    $NewTime = mktime(date('G'), date('i'), date('s'), date('n'), date('j') + $DaysToAdd, date('Y'));

    From mktime documentation:

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.

    The advantage of this method is that you can add or subtract any time interval (hours, minutes, seconds, days, months, or years) in an easy to read line of code.

    Beware there is a tradeoff in performance, as this code is about 2.5x slower than strtotime("+1 day") due to all the calls to the date() function. Consider re-using those values if you are in a loop.

    0 讨论(0)
  • 2020-12-04 19:52
    <?php
    $dt = new DateTime;
    if(isset($_GET['year']) && isset($_GET['week'])) {
        $dt->setISODate($_GET['year'], $_GET['week']);
    } else {
        $dt->setISODate($dt->format('o'), $dt->format('W'));
    }
    $year = $dt->format('o');
    $week = $dt->format('W');
    ?>
    
    <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week-1).'&year='.$year; ?>">Pre Week</a> 
    <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week+1).'&year='.$year; ?>">Next Week</a>
    <table width="100%" style="height: 75px; border: 1px solid #00A2FF;">
    <tr>
    <td style="display: table-cell;
        vertical-align: middle;
        cursor: pointer;
        width: 75px;
        height: 75px;
        border: 4px solid #00A2FF;
        border-radius: 50%;">Employee</td>
    <?php
    do {
        echo "<td>" . $dt->format('M') . "<br>" . $dt->format('d M Y') . "</td>\n";
        $dt->modify('+1 day');
    } while ($week == $dt->format('W'));
    ?>
    </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-04 19:53

    The date_add() function should do what you want. In addition, check out the docs (unofficial, but the official ones are a bit sparse) for the DateTime object, it's much nicer to work with than the procedural functions in PHP.

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