简体   繁体   English

PHP时间戳记的总剩余小时数不同

[英]PHP timestamp different in total remaining hour

i am implementing one application in which i have to display a time difference in total hour from end date to start date. 我正在实现一个应用程序,其中我必须显示从结束日期到开始日期的总小时数的时差。 suppose i have both start date and end date in timestamp.so how i will get the difference of date in total hour ? 假设我在timestamp。既有开始日期又有结束日期,那么我将如何获得总小时数中的日期差? now i am using the below code 现在我正在使用下面的代码

 $intervalo = date_diff(date_create(), date_create($end));
    pr($intervalo);

and the output is like 和输出就像

DateInterval Object
(
    [y] => 0
    [m] => 1
    [d] => 4
    [h] => 18
    [i] => 41
    [s] => 2
    [invert] => 0
    [days] => 34
)

the above code show me the total hour baut the end date is greater then 1month it shows 1m in array . 上面的代码向我展示了总的小时结束日期大于1月,它显示了1m的数组。 but i only want total number of hour only can any one help me ? 但是我只希望总小时数能帮助我吗? thanks in advance 提前致谢

Don't use date_diff . 不要使用date_diff Convert the dates to timestamps (which represent seconds), subtract the two, and divide by 3600 . 将日期转换为时间戳(代表秒),将两者相减并除以3600

$x = date_create();
$y = date_create($end)
$hours = ($y->getTimestamp() - $x->getTimestamp()) / 3600;

If you want hours, minutes, and seconds: 如果您需要小时,分钟和秒:

$x = date_create();
$y = date_create($end)
$diff = ($y->getTimestamp() - $x->getTimestamp());

$seconds = $diff % 60;
$diff    = (int)($diff / 60);
$minutes = $diff % 60;
$diff    = (int)($diff / 60);
$hours   = $diff;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM