Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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_Time - Fatal编程技术网

Php 用时间戳计算时间

Php 用时间戳计算时间,php,time,Php,Time,对不起,这个标题有点误导人,我真的不知道该怎么说 基本上,我在系统中使用一个时钟 我得到了每个时钟输入的数据:时钟输入时间戳和时钟输出时间戳 在特定时间段内显示所有时钟后,脚本将两个时间戳之间的所有差异相加 我现在需要做的一件事,就是把这个数字转换成小时和分钟 到目前为止,您已经工作了数小时和数分钟 <strong>So far, you have worked <?php $hours = floor($i / 3600); $i -= 3600 * floor($i /

对不起,这个标题有点误导人,我真的不知道该怎么说

基本上,我在系统中使用一个时钟

我得到了每个时钟输入的数据:时钟输入时间戳和时钟输出时间戳

在特定时间段内显示所有时钟后,脚本将两个时间戳之间的所有差异相加

我现在需要做的一件事,就是把这个数字转换成小时和分钟

到目前为止,您已经工作了数小时和数分钟
<strong>So far, you have worked  <?php
$hours =  floor($i / 3600);
$i -= 3600 * floor($i / 3600);
$minutes  = floor($i / 60);
echo $hours; ?> hours and  <?php echo $minutes; ?> minutes</strong>

是的,它可以工作。

从更详细的输出中使用此函数

function datefor($date, $time)
 {
$days = floor($time / (60 * 60 * 24));
$remainder = $time % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;

if($days > 0) {
        $data = date('F d Y', $date);
        reset($date);
    }     
    elseif($days == 0 && $hours == 0 && $minutes == 0) {
        $data = "few seconds ago";
    }
    elseif($days == 0 && $hours == 0) {
        $data = $minutes.' minutes ago';
    }
    elseif($days == 0 && $hours > 0) {
        $data = $hours.' hour ago';
    }
    else {
        $data = "few seconds ago";
    }        

    return $data;
}
并使用以下语句选择

SELECT *,UNIX_TIMESTAMP() - datetime AS TimeSpent FROM `table_name`;

$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){ 
//调用函数

$check = datefor($rows['datetime'], $rows['TimeSpent']);}

现在回显$check;您希望显示时间的位置。

所以您有一个最终的总和,即秒数,对吗?你可以通过除以60将其转换为分钟,再除以60将其转换为小时……我刚刚意识到-谢谢:)