我有一个以分钟为单位的日期差,然后在1小时后变为数小时..然后在24小时后变为数天..然后在30天后又变回了小时数
为什么?!我在这段代码中缺少什么,或者这是一个错误?!
$d_start = new DateTime('now');
$d_end = new DateTime($last_gdate);
$d_dif = $d_start->diff($d_end);
$d_d = $d_dif->d;
$d_h = $d_dif->h;
$d_m = $d_dif->i;
if ($d_d < 1) {
if ($d_h < 1) {
$d_since = $d_m . "m";
} else {
$d_since = $d_h . "h";
}
} else {
$d_since = $d_d . "d";
}
答案 0 :(得分:2)
您会注意到DateTime的diff()
将在下个月重新启动d
。如果您不打算检查/兑现days
,则应该使用m
。
代码:(Demo)
$last_gdate = "2018-07-23 00:01:02";
$d_start = new DateTime('now');
$d_end = new DateTime($last_gdate);
$d_dif = $d_start->diff($d_end);
var_export($d_dif);
$d_d = $d_dif->days;
$d_h = $d_dif->h;
$d_m = $d_dif->i;
if ($d_d < 1) {
if ($d_h < 1) {
$d_since = $d_m . "m";
} else {
$d_since = $d_h . "h";
}
} else {
$d_since = $d_d . "d";
}
echo "\n$d_since";
输出:
DateInterval::__set_state(array(
'y' => 0,
'm' => 1,
'd' => 0,
'h' => 6,
'i' => 42,
's' => 2,
'f' => 0.002468,
'weekday' => 0,
'weekday_behavior' => 0,
'first_last_day_of' => 0,
'invert' => 1,
'days' => 31,
'special_type' => 0,
'special_amount' => 0,
'have_weekday_relative' => 0,
'have_special_relative' => 0,
))
31d