Get date in php in 2 weeks

独自空忆成欢 提交于 2021-02-07 11:48:44

问题


$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.

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


回答1:


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

See strtotime().


Update #1: Yes, this is the lazy way to do it. However, I do believe that the possible negative 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).


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

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



回答2:


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

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

 $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:

 $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:

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



回答3:


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;


来源:https://stackoverflow.com/questions/3582435/get-date-in-php-in-2-weeks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!