PHP mysql中的时间戳差异

时间:2014-05-31 11:53:57

标签: php mysql time difference

我想获得两个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时,它会显示一些垃圾值而不是我期望的值。

请拜托。

1 个答案:

答案 0 :(得分:0)

以下是此问题的正确答案

  $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()。

相关问题