如何在PHP中获得两天的差异

时间:2015-04-17 06:19:28

标签: php date datetime datediff

我有一些传递类似参数的函数

everyWeekOn("Mon",11,19,00)

我想计算当天之间的差异(例如'周五') 并传递参数日,即Mon

输出应为:

The difference between Mon and Fri is 3

我试过这样的

 $_dt = new DateTime();
 error_log('$_dt date'. $_dt->format('d'));
 error_log('$_dt year'. $_dt->format('Y'));
 error_log('$_dt month'. $_dt->format('m'));

但是知道我不知道接下来要做什么来获得两天之间的差异。

请注意,此问题与How to calculate the difference between two dates using PHP?不同,因为我只有一天而非完整日期。

3 个答案:

答案 0 :(得分:3)

只需与DateTime方法一起实施->diff课程:

function everyWeekOn($day) {
    $today = new DateTime;
    $next = DateTime::createFromFormat('D', $day);
    $diff = $next->diff($today);
    return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}

echo everyWeekOn('Mon');

答案 1 :(得分:1)

$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';

答案 2 :(得分:0)

你可以找不到。使用此代码的两天内的天数

<?php
    $today = time(); 
    $chkdate = strtotime("16-04-2015");
    $date = $today - $chkdate;
    echo floor($date/(60*60*24));
?>

请使用这可以帮助您