Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中如何计算缺勤时间_Php_Arrays_While Loop_Counting - Fatal编程技术网

在php中如何计算缺勤时间

在php中如何计算缺勤时间,php,arrays,while-loop,counting,Php,Arrays,While Loop,Counting,我想知道每个学生的缺勤时间 例如: 我想得到$student\u deposition\u from\u time和$student\u deposition\u to\u time之间的差异,然后计算差异,得到每个学生的总缺勤时间,如下10:00-08:00=2小时+10:00-08:00=2h+09:00-08:00=1小时总缺勤时间为5小时 谢谢:)获取并存储时间戳作为函数time()(自1970年起的秒数)的结果,而不是作为文本。这个时间独立于任何时区。两个时间戳之间的差异是消失的秒数。

我想知道每个学生的缺勤时间

例如:

我想得到
$student\u deposition\u from\u time
$student\u deposition\u to\u time
之间的差异,然后计算差异,得到每个学生的总缺勤时间,如下10:00-08:00=2小时+10:00-08:00=2h+09:00-08:00=1小时总缺勤时间为5小时
谢谢:)

获取并存储时间戳作为函数
time()
(自1970年起的秒数)的结果,而不是作为文本。这个时间独立于任何时区。两个时间戳之间的差异是消失的秒数。使用
date()
strftime()
(我更喜欢秒)将时间戳打印为文本

顺便说一句,
UNIX\u TIMESTAMP()
是与
time()
相当的SQL,您可以使用它来转换时间,然后对时间进行计数

考虑以下几点:

$arr = array();
$arr[] = array("student_absence_from_time" => "08:00", "student_absence_to_time" => "10:00");
$arr[] = array("student_absence_from_time" => "09:00", "student_absence_to_time" => "10:00");
$arr[] = array("student_absence_from_time" => "08:00", "student_absence_to_time" => "09:00");

function cntAbsence($carry, $item) {
    return $carry +  strtotime($item["student_absence_to_time"]) - strtotime($item["student_absence_from_time"]);
} // adding the different between "from_time" to "to_time"
echo array_reduce($arr, "cntAbsence", 0) / 3600; // time is in second so divided by 3600 to get hours and use 0 as init

使用类似于
$datetimeFrom->diff($datetimeTo)的东西签出Datetime问题是什么?老实说,这是非常基本的编程。我建议你将问题分解,并尝试研究如何完成你不理解的每一步。我只想知道如何计算每个学生的总缺勤时间。非常感谢,这是正确的工作,只需删除$arr=array();
$arr = array();
$arr[] = array("student_absence_from_time" => "08:00", "student_absence_to_time" => "10:00");
$arr[] = array("student_absence_from_time" => "09:00", "student_absence_to_time" => "10:00");
$arr[] = array("student_absence_from_time" => "08:00", "student_absence_to_time" => "09:00");

function cntAbsence($carry, $item) {
    return $carry +  strtotime($item["student_absence_to_time"]) - strtotime($item["student_absence_from_time"]);
} // adding the different between "from_time" to "to_time"
echo array_reduce($arr, "cntAbsence", 0) / 3600; // time is in second so divided by 3600 to get hours and use 0 as init