PHP使用TIME从数据库计算小时 [英] PHP using TIME from database to calculate hours

查看:155
本文介绍了PHP使用TIME从数据库计算小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
require 'dbfunction.php';
$con = getDbConnect();

if (!mysqli_connect_errno($con)) {

                $queryStr = "SELECT * " .
                        "FROM crewlist WHERE crew_name = 'Peter'";
            }
            $result = mysqli_query($con, $queryStr);
            while ($row = mysqli_fetch_array($result)) {

                $a = new DateTime($row["start_hour"]);
                $b = new DateTime($row["end_hour"]);
                $shift1 = $a->diff($b);

                $c = new DateTime($row["start_hour2"]);
                $d = new DateTime($row["end_hour2"]);
                $shift2 = $c->diff($d);

                if ($shift1 > 60*60*14) {
                    echo 'no';
                }
  ?>

我试图推断我的换档时间之间的差异,条件为不超过14小时)。提前致谢。

I am trying to deduce the difference between my shift timings, with the condition of (for e.g. shift 1 not exceeding 14 hours). Thanks in advance. The fields of my form is using time as the type.

推荐答案

转换您的开始日期结束日期转换为时间戳,然后使用两者的差异。

Convert your start date and end date into timestamp and then take difference of both.

$diff = abs($start-$end)
if($diff > 60 * 60 *14 ){
 // Your desired statement
}

如果该差异超过60 * 60 * 14,那么它将超过14小时。

if that difference exceeds to 60*60*14 then it will exceed to 14 hour.

这篇关于PHP使用TIME从数据库计算小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆