簡體   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