返回当前日期加 7 天



我正在尝试获取当前日期加上要显示的 7 天。

示例:今天是 2012 年 8 月 16 日,因此此 php 代码段将输出 2012 年 8 月 23 日。

   $date = strtotime($date);
   $date = strtotime("+7 day", $date);
   echo date('M d, Y', $date);

现在,我得到的是:1970年1月8日。我错过了什么?

strtotime将自动使用当前的Unix时间戳来基于您的字符串注释。

只需做:

$date = strtotime("+7 day");
echo date('M d, Y', $date);

为未来访客添加了信息:如果您需要将时间戳传递给函数,以下内容将起作用。

这将计算昨天的7 days

$timestamp = time()-86400;
$date = strtotime("+7 day", $timestamp);
echo date('M d, Y', $date);
$date = new DateTime(date("Y-m-d"));
$date->modify('+7 day');
$tomorrowDATE = $date->format('Y-m-d');

如果您要查找的是 7 天后,只需输入:

$date = strtotime("+7 day", time());
echo date('M d, Y', $date);
$now = date('Y-m-d');
$start_date = strtotime($now);
$end_date = strtotime("+7 day", $start_date);
echo date('Y-m-d', $start_date) . '  + 7 days =  ' . date('Y-m-d', $end_date);
<?php
print date('M d, Y', strtotime('+7 days') );

您没有使用 time() 函数,该函数返回自 Unix 时代(1970 年 1 月 1 日 00:00:00 GMT)以来以秒数测量的当前时间。像这样使用:

$date = strtotime(time());
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);

这段代码对我有用:

<?php
$date = "21.12.2015";
$newDate = date("d.m.Y",strtotime($date."+2 day"));
echo $newDate; // print 23.12.2015
?>
echo date('d-m-Y', strtotime('+7 days'));
$date = strtotime("+7 day", strtotime("M d, Y"));
$date =  date('j M, Y', $date);

这也将起作用

以下是使用 strtotime() 的方法

<?php
    $date = strtotime("3 October 2005");
    $d = strtotime("+7 day", $date);
    echo "Created date is " . date("Y-m-d h:i:sa", $d) . "<br>";
?>

最新更新