在 2 周内在 php 中获取日期

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

Get date in php in 2 weeks

phpdatetime

提问by anonymous123

$addhr = time() + (1 * 13 * 60 * 60);
$curDateTimeMil= date("Y-m-d G:i:s",$addhr);
echo $curDateTimeMil; 

This will echo 2010-08-27 16:21:31.

这将回声2010-08-27 16:21:31

How can I get the date after 2 weeks? (sept 10)

我怎样才能在 2 周后获得日期?(9 月 10 日)

回答by jensgram

$dateInTwoWeeks = strtotime('+2 weeks');

See strtotime().

strtotime()



Update #1: Yes, this is the lazy way to do it. However, I do believe that the possiblenegative performance impact is countered by the positive effect on readability / understanding of the code. Should performance be an issue, one could switch to native (integer-based) time manipulation (and add comments).

更新 #1:是的,这是一种懒惰的方法。但是,我确实相信可能的负面性能影响会被对代码可读性/理解的积极影响所抵消。如果性能是一个问题,可以切换到本机(基于整数)时间操作(并添加注释)。



Update #2: The optional second argument is the reference date. E.g.:

更新 #2:可选的第二个参数是参考日期。例如:

strtotime('+2 weeks', mktime(0, 0, 0, 2, 8, 1984)); // 8th Feb. 1984 + 2 weeks

回答by shamittomar

You can specify the starting time (from where to calculate) using mktime().

您可以使用 指定开始时间(从哪里计算)mktime()

Example: Two week after September 10, 2010 (i.e. +14 days):

示例:2010 年 9 月 10 日之后的两周(即 +14 天):

 $date = date("Y-m-d", mktime(0, 0, 0, 9, 10 + 14, 2010);

To get just the DATE (not time) of two weeks later (+14 days) from today:

从今天起仅获取两周后(+14 天)的 DATE(而非时间):

 $date = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+14, date("Y")));

And this gives with TIME of two weeks later (+14 days) from now:

这是从现在起两周后(+14 天)的 TIME:

 $date = date("Y-m-d G:i:s", mktime(date("G"), date("i"), date("s"), date("m"), date("d")+14, date("Y")));

回答by Gumbo

You could simply add the number seconds of two weeks:

您可以简单地添加两周的秒数:

2 weeks = 2 · 7 days = 14 days
        = 14 · 24 hours = 336 hours
        = 336 · 60 minutes = 20160 minutes
        = 20160 · 60 seconds = 1209600 seconds

So:

所以:

$curDateTimeMil += 1209600;
// or
$curDateTimeMil += 2 * 7 * 24 * 60 * 60;