php 返回当前日期加 7 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11988428/
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-25 02:34:31  来源:igfitidea点击:

Return current date plus 7 days

phpdate

提问by James Clear

I'm Trying to get the current date plus 7 days to display.

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

Example: Today is August 16, 2012, so this php snippet would output August 23, 2012.

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

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

Right now, I'm getting: Jan 08, 1970. What am I missing?

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

回答by Mike Mackintosh

strtotimewill automatically use the current unix timestamp to base your string annotation off of.

strtotime将自动使用当前的 unix 时间戳作为您的字符串注释的基础。

Just do:

做就是了:

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

Added Info For Future Visitors:If you need to pass a timestamp to the function, the below will work.

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

This will calculate 7 daysfrom yesterday:

7 days将从昨天开始计算:

$timestamp = time()-86400;

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

回答by Abduhafiz

$date = new DateTime(date("Y-m-d"));
$date->modify('+7 day');
$tomorrowDATE = $date->format('Y-m-d');

回答by wesside

If it's 7 days from now that you're looking for, just put:

如果您正在寻找 7 天后,只需输入:

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

回答by Mahmoud Zalt

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

回答by MetalFrog

<?php
print date('M d, Y', strtotime('+7 days') );

回答by MKT

you didn't use time()function that returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). use like this:

您没有使用time()函数返回自 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的秒数测量的当前时间。像这样使用:

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

回答by LeonZo

This code works for me:

这段代码对我有用:

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