简体   繁体   English

将当前日期增加5天

[英]Increase current date by 5 days

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

How do I change $date to $date + 5 days ? 如何将$date更改$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
$date = date('Y-m-d', strtotime('+5 days'));

You could use mktime() using the timestamp. 您可以使用mktime()时间戳的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. 使用strtotime()速度更快,但是我的方法仍然有效,并且在需要进行大量修改的情况下非常灵活。 Plus, strtotime() can't handle ambiguous dates. 另外, 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() . 如果您必须以YYYY-MM-DD格式向已存在的日期字符串添加5天,则可以将其拆分为一个数组,然后将这些部分与mktime()

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

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

strtotime() is very nice. strtotime()非常好。 It allows you to do the following: 它允许您执行以下操作:

$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 现在=> 1360334498 = 2013-02-08

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

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

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

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

or: 要么:

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

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

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

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

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

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

使用strtotime

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

You can use 您可以使用

strtotime(“+5 days”)

to get the current date plus 5 days or 获取当前日期加上5天,或者

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

I used this: 我用这个:

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

It's working now. 现在正在工作。

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;

如果日期已经存在,则可以使用以下代码:

$tartDate = date("m/d/Y", strtotime("+1 Day", strtotime($Date)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM