gpt4 book ai didi

php - PHP mysql 中的时间戳差异

转载 作者:行者123 更新时间:2023-11-30 00:12:01 24 4
gpt4 key购买 nike

我想获得两个 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 ..

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

如下

代码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 工作完美,但是当我尝试执行代码 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(abs($d2 - $d1) / 60);
$d = floor ($all / 1440);
$h = floor (($all - $d * 1440) / 60);
$m = $all - ($d * 1440) - ($h * 60);

在那里使用abs()。

关于php - PHP mysql 中的时间戳差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com