简体   繁体   English

PHP MySQL中的时间戳差异

[英]Timestamp difference in PHP mysql

I want to get the exact time difference between two php timestamps. 我想得到两个PHP时间戳之间的确切时间差。 & I m using this code 我正在使用此代码

CODE 1: 代码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);

& if print this $h & $m - I get what exactly I m looking for , difference between two timestamps. &如果打印此$h & $m我得到的正是我想要的东西,两个时间戳之间存在差异。

Now but I want to get $d2 from my table .. 现在,但是我想从我的桌子上拿$d2 ..

I have stored a timestamp in mysql table as datatype TIME . 我已将时间戳存储在mysql表中,数据类型为TIME。

as below 如下

CODE 2 代码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);

But when I print the $h & $m then im not getting the values properly... Please any one help me to do so... 但是当我打印$h & $m我无法正确获取值...请任何人帮助我...

Code 1 works perfectly but when i try to execute Code2 it shows some garbage values not the values im expecting. 代码1完美地工作,但是当我尝试执行Code2时,它显示了一些垃圾值,而不是我期望的值。

please please please. 请拜托。

Following is the correct answer for this question 以下是此问题的正确答案

  $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);

USE abs() over there. 在那儿使用abs()。

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

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