如何在 php 中获得两天之间的差异
How to get difference between two day in php
我有一个传递一些参数的函数,比如
everyWeekOn("Mon",11,19,00)
我想计算当天之间的差异(例如 'Fri')
并传递了参数日,即 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?不同,因为我只有一天,而不是一个完整的日期。
你找不到。使用此代码
两天内的天数
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
请使用这个可能对你有帮助
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
只需将 DateTime
class 与 ->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');
我有一个传递一些参数的函数,比如
everyWeekOn("Mon",11,19,00)
我想计算当天之间的差异(例如 'Fri')
并传递了参数日,即 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?不同,因为我只有一天,而不是一个完整的日期。
你找不到。使用此代码
两天内的天数<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
请使用这个可能对你有帮助
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
只需将 DateTime
class 与 ->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');