如何在特定周后找到日期?

时间:2009-10-31 06:40:24

标签: php

我需要在一定数周之后找到一个日期。例如,如果开始日期是2009年10月31日星期六,如果我选择16周,那么我需要在16个星期六之后找到日期。

提前致谢。

4 个答案:

答案 0 :(得分:5)

您可以使用strtotime

// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20


// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07

答案 1 :(得分:2)

strtotime()就是你想要的:它需要一个表达式并将其转换为日期对象。另请参阅date()

$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)

答案 2 :(得分:1)

如果您使用的是PHP 5.2.0或更高版本,则可以使用DateTime class

<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.

// Now add 16 weeks
$date->modify('+16 weeks');

// Now you can output it however you wish
echo $date->format('Y-m-d');
?>

答案 3 :(得分:0)

为什么不使用unix时间,从中减去7 * 24 * 3600 * 16,然后将其转换回您需要的日期。

这有助于转换回来: http://php.net/manual/en/function.date.php