【问题标题】:Calculate the hours minutes and days of difference between two date and hours计算两个日期和小时之间的小时分钟和天数
【发布时间】:2019-12-30 23:45:03
【问题描述】:

我有一个 PHP 和 MySQL 代码应该计算两个日期和小时之间的小时分钟和天数。效果很好,只是比平时多增加了 20 小时 20 分钟。我删除了 DATE 部分并手动输入日期和时间,它工作正常。

我不明白会发生什么。

    $fecha = $row["fecha"];
        $data= $row["hora"];
$start = strtotime("$fecha $hora"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}

编辑

2019-12-29 21:27:50 开始。 2019 年 12 月 31 日结束 0:51:50 = 47:51:16

如你所见,它的计算很糟糕。

【问题讨论】:

  • if ($statusfichaje == Start)
  • 生成$end的代码应该简化为$end = strtotime('now');
  • 检查,它也不起作用。这让我多花 20 个小时
  • 我们在这里不使用 [已解决的] 标题黑客 - 而是有一个问题接受系统。单击最能帮助您的答案旁边的勾号,使其变为绿色。 (欢迎自行回答,但我更愿意接受其他人的回答,如果他们帮助我找到解决方案的大部分方法)。

标签: php datetime


【解决方案1】:

这样一个简单的例子就可以完成这项工作:

$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;

这是你应该工作的代码

$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}

【讨论】:

  • 以你的例子,它的发生方式与它让我多花 20 个小时的方式相同。
  • $statusfichaje == start) 这将为您提供真实的答案。
【解决方案2】:

问题出在字符串中。

  $data= $row["hora"];

我用这个

$start = strtotime("$fecha $hora");

不要只花时间计算几天。

谢谢

【讨论】:

  • $start = strtotime("$fecha $hora");复制了我的答案
猜你喜欢
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 2012-05-19
  • 2016-11-07
  • 1970-01-01
  • 2012-10-26
  • 2016-02-16
  • 1970-01-01
相关资源
最近更新 更多