php 将当前日期增加 5 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8307963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:23:35  来源:igfitidea点击:

Increase current date by 5 days

phpdate

提问by Jasper

$date = date('Y-m-d',current_time('timestamp', 0));

How do I change $dateto $date + 5 days?

我如何更改$date$date + 5 days

PHP version is 5.2.

PHP 版本是 5.2。

This code doesn't work:

此代码不起作用:

$date_cur = date('Y-m-d',current_time('timestamp', 0));
echo $date_cur . ' <br>';
$date_cur_plus = date($date_cur, strtotime('+5 days'));
echo $date_cur_plus;

Gives me:

给我:

2011-11-29 
2011-11-29

回答by matino

$date = date('Y-m-d', strtotime('+5 days'));

回答by Yes Barry

You could use mktime()using the timestamp.

您可以使用mktime()时间戳。

Something like:

就像是:

$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y')));

Using strtotime()is faster, but my method still works and is flexible in the event that you need to make lots of modifications. Plus, strtotime()can't handle ambiguous dates.

使用strtotime()速度更快,但我的方法仍然有效,并且在您需要进行大量修改时很灵活。另外,strtotime()无法处理不明确的日期。

Edit

编辑

If you have to add 5 days to an already existing date string in the format YYYY-MM-DD, then you could split it into an array and use those parts with mktime().

如果您必须向格式中现有的日期字符串添加 5 天YYYY-MM-DD,那么您可以将其拆分为一个数组并将这些部分与mktime().

$parts = explode('-', $date);
$datePlusFive = date(
    'Y-m-d', 
    mktime(0, 0, 0, $parts[1], $parts[2] + 5, $parts[0])
    //              ^ Month    ^ Day + 5      ^ Year
);

回答by Turdaliev Nursultan

Object oriented Style:

面向对象风格:

<?php
    $date = new DateTime('now');
    $date->add(new DateInterval('P5D'));
    echo $date->format('Y-m-d') . "\n";
?>

Procedural Style:

程序风格:

<?php
    $date = date_create('2016-01-01');
    date_add($date, date_interval_create_from_date_string('5 days'));
    echo date_format($date, 'Y-m-d');
?>

回答by temuraru

strtotime()is very nice. It allows you to do the following:

strtotime()是非常好的。它允许您执行以下操作:

$startDate = 'now'; // or choose a certain date you want/have
$startDate = '2013-02-14';
$intervals = array(
    '', 
    '+ 5 days', 
    '+ 31 days', 
    '+ 3 months', 
    '+ 2 years + 2 days'
); 
foreach($intervals as $interval) {
    $combinedDate = $startDate . ' ' . $interval;
    var_dump($combinedDate . ' => ' date('Y-m-d', strtotime($combinedDate)));
}

with a result:

结果:

now => 1360334498 = 2013-02-08

now + 5 days => 1360766498 = 2013-02-13

now + 31 days => 1363012898 = 2013-03-11

now + 3 months => 1368020498 = 2013-05-08

now + 2 years + 2 days => 1423579298 = 2015-02-10

现在 => 1360334498 = 2013-02-08

现在 + 5 天 => 1360766498 = 2013-02-13

现在 + 31 天 => 1363012898 = 2013-03-11

现在 + 3 个月 => 1368020498 = 2013-05-08

现在 + 2 年 + 2 天 => 1423579298 = 2015-02-10

or:

或者:

2013-02-14 => 1360792800 = 2013-02-14

2013-02-14 + 5 days => 1361224800 = 2013-02-19

2013-02-14 + 31 days => 1363471200 = 2013-03-17

2013-02-14 + 3 months => 1368478800 = 2013-05-14

2013-02-14 + 2 years + 2 days => 1424037600 = 2015-02-16

2013-02-14 => 1360792800 = 2013-02-14

2013-02-14 + 5 天 => 1361224800 = 2013-02-19

2013-02-14 + 31 天 => 1363471200 = 2013-03-17

2013-02-14 + 3 个月 => 1368478800 = 2013-05-14

2013-02-14 + 2 年 + 2 天 => 1424037600 = 2015-02-16

回答by phihag

Use strtotime:

使用strtotime

$date = date('Y-m-d', strtotime('+5 days'));

回答by Francis Avila

$dateplus5 = date('Y-m-d', strtotime('+5 days'));

回答by Taz

You can use

您可以使用

strtotime(“+5 days”)

to get the current date plus 5 days or

获取当前日期加上 5 天或

$targetDate = date($date, strtotime('+5 days'));

回答by Irfan Hafid Nugroho

For specific date:

具体日期:

$date = '2011-11-01';
$date_plus = date('Y-m-d', strtotime($date.'+5 days'));
echo $date.'<br>'.$date_plus;

It will be give :

它将给:

2011-11-01
2011-11-06

回答by bharati bane

I used this:

我用过这个:

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

It's working now.

它现在正在工作。

回答by Lafif Astahdziq

Did not supposed to be like this?

不应该是这样吗?

$date_cur = date('Y-m-d', current_time('timestamp', 0));
echo $date_cur . ' <br>';
$date_cur_plus = date('Y-m-d', strtotime('+5 days', current_time('timestamp', 0) ) );
echo $date_cur_plus;