在php中添加几天到当前日期

时间:2016-11-02 07:55:44

标签: php

我有这段代码

<?php
$date =date(Y-m-d);
$day = 5;
$newdate= $date+$day
echo "today is:"$date;
echo "<br> and after 5 days is :"$newdate;
?>

我想要的结果是 今天是:2016-11-2 5天后是:2016-11-7

4 个答案:

答案 0 :(得分:3)

它可以帮到你:

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

所以它将如下:

<?php
$date = date('Y-m-d');
$newdate = date('Y-m-d', strtotime($date.' + 5 days'));
echo "today is: $date";
echo "<br> and after 5 days is: $newdate";
?>

答案 1 :(得分:2)

试试这个

$date    = new DateTime();               // Creates new DatimeTime for today
$newdate = $date->modify( '+ 5 days' );  // Adds 5 days
echo $newdate->format( 'Y-m-d' );        // Echo and format the newdate to the wanted format

答案 2 :(得分:0)

它可以帮到你

{{1}}

答案 3 :(得分:0)

您可以使用strtotime()函数将天数添加到当前日期。请参阅以下内容:

 <?php
   $date =date("Y-m-d");
   $day = 5;
   $newdate=date('Y-m-d', strtotime("+$day days"));
   echo "today is:".$date;
   echo "<br> and after 5 days is :".$newdate;
 ?>