Php 获取相对于特定日期的下一个星期四

Php 获取相对于特定日期的下一个星期四,php,date,Php,Date,如果通过的日期不是星期四,我想得到下一个星期四相对于特定日期的时间。这就是我们对代码的理解 <?php $string = '2014.07.16'; $date = DateTime::createFromFormat("Y.m.d", $string); $result = $date->format("D"); if ($result != 'Thu') { $result2 = strtotime('next Thursday'); echo date('Y.m.d',

如果通过的日期不是星期四,我想得到下一个星期四相对于特定日期的时间。这就是我们对代码的理解

<?php 

$string = '2014.07.16';
$date = DateTime::createFromFormat("Y.m.d", $string);
$result = $date->format("D");
if ($result != 'Thu')
{
$result2 = strtotime('next Thursday');
echo date('Y.m.d', $result2);
}
?>

在上面的代码中,变量$string是传递的日期,它是一个“Wed” 我想得到“2014.07.16”,这是相对于“2014.07.17”的下一个星期四。 这意味着如果$string是'2014.07.21',这是一个Mon,那么返回的日期将是'2014.07.24',这是相对于'2014.07.21'的下一个星期四 我现在得到的是“2014.07.31”,即今天的下一个星期四

如何获取相对于$string(已传递日期)的下一个星期四


谢谢。

strotime非常智能,可以处理各种参数。但是,您需要以不同于当前使用的格式传递日期。只要更换。with-将日期传递给strotime之前:

$string = '2014.07.16';
$string = str_replace('.', '-', $string);

if (date('w', strtotime($string)) != 4) {
    $date = date('Y.m.d', strtotime($string . ' next thursday'));
    echo $date;
}
date('w')将一周中的某一天返回为整数。使用它进行比较比使用字符串更好。

试试这个

$string = '2014.07.16';
$date = DateTime::createFromFormat("Y.m.d", $string);
$result = $date->format("D");
if ($result != 'Thu')
{
    $result2 = strtotime('next Thursday', strtotime($date->format("d-M-Y")));
    echo date('Y.m.d', $result2);
}
输出:2014.07.17


$result!='Thu'->$result!='Thu'Try
$result=$date->format(“W”)检查你的措辞。”2014.07.16’,相对于2014.07.16,这是下一个星期四。谢谢。这正是我所需要的。谢谢彼得,你的也是一个很好的考虑。