繁体   English   中英

PHP MySQL中的时间戳差异

[英]Timestamp difference in PHP mysql

我想得到两个PHP时间戳之间的确切时间差。 我正在使用此代码

代码1:

   $d1=strtotime(date('H:i:s'));      //current time consider e.g 15:00:00
   $d2=strtotime('18:00:00');

   $all = round(($d2 - $d1) / 60);
   $d = floor ($all / 1440);
   $h = floor (($all - $d * 1440) / 60);
   $m = $all - ($d * 1440) - ($h * 60);

&如果打印此$h & $m我得到的正是我想要的东西,两个时间戳之间存在差异。

现在,但是我想从我的桌子上拿$d2 ..

我已将时间戳存储在mysql表中,数据类型为TIME。

如下

代码2

    $d1=strtotime(date('H:i:s'));      //current time consider e.g 15:00:00

$d2=strtotime($rows['showtime']);  //where $rows['showtime'] is the timestamp stored in mysql table. consider it as 18:00:00

$all = round(($d2 - $d1) / 60);
$d = floor ($all / 1440);
$h = floor (($all - $d * 1440) / 60);
$m = $all - ($d * 1440) - ($h * 60);

但是当我打印$h & $m我无法正确获取值...请任何人帮助我...

代码1完美地工作,但是当我尝试执行Code2时,它显示了一些垃圾值,而不是我期望的值。

请拜托。

以下是此问题的正确答案

  $d1=strtotime(date('H:i:s'));      //current time consider e.g 15:00:00

  $d2=strtotime($rows['showtime']);  //where $rows['showtime'] is the timestamp stored in mysql table. consider it as 18:00:00

  $all = round(abs($d2 - $d1) / 60);
  $d = floor ($all / 1440);
  $h = floor (($all - $d * 1440) / 60);
  $m = $all - ($d * 1440) - ($h * 60);

在那儿使用abs()。

暂无
暂无

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

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