将天数增加到 php 当前 Date()

发布于 2024-07-08 07:26:05 字数 120 浏览 9 评论 0原文

如何在 PHP 中向当前日期添加一定天数?

我已经得到了当前日期:

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

只需添加 x 天数即可

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');

Just need to add x number of days to it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

滿滿的愛 2024-07-15 07:26:05

php 支持 C 风格的日期函数。 您可以通过 strtotime 函数使用英语风格短语添加或减去日期周期。 例子...

$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'));

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

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

窗影残 2024-07-15 07:26:05

一天是86400秒。

$tomorrow = date('y:m:d', time() + 86400);

a day is 86400 seconds.

$tomorrow = date('y:m:d', time() + 86400);
青朷 2024-07-15 07:26:05

最简单的方法是添加x号。 天数..

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

或从指定日期开始...

echo date('Y-m-d',strtotime('+1 day', strtotime('2007-02-28')));

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')));
淡淡離愁欲言轉身 2024-07-15 07:26:05

使用 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;

将输出

2012-12-24

2012-12-25

2012-12-26

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

赠意 2024-07-15 07:26:05

date_add() 函数应该做你想做的事。 此外,请查看文档(非官方的,但官方的有点稀疏) DateTime 对象,它比 PHP 中的过程函数好用得多。

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.

月光色 2024-07-15 07:26:05

如果您在多个地方需要此代码,那么我建议您添加一个简短的函数,以使您的代码更简单且更易于测试。

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

然后你可以在任何地方使用它,如下所示:

$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 );

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 );
哑剧 2024-07-15 07:26:05
$NewDate=Date('Y-m-d', strtotime('+365 days'));

回显$NewDate; //2020-05-21

$NewDate=Date('Y-m-d', strtotime('+365 days'));

echo $NewDate; //2020-05-21

把人绕傻吧 2024-07-15 07:26:05

将 15 天添加到选择元素(使用“Alive to Die”建议)

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

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

青萝楚歌 2024-07-15 07:26:05

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

来自 mktime 文档

mktime() 对于进行日期算术和验证非常有用,因为它会自动计算超出范围的输入的正确值。

此方法的优点是您可以在易于阅读的代码行中添加或减去任何时间间隔(小时、分钟、秒、天、月或年)。

请注意,这会影响性能,因为由于所有对 date() 函数的调用,此代码比 strtotime("+1 day") 慢约 2.5 倍。 如果您处于循环中,请考虑重新使用这些值。

$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.

你又不是我 2024-07-15 07:26:05

您还可以使用面向对象编程 (OOP) 来代替过程编程:

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

或者仅使用一行代码:

$fiveDaysAgo = (new DateTime())->sub(new DateInterval('P5D'));

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'));
纸短情长 2024-07-15 07:26:05
<?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>
<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文