用时间戳计算时间

时间:2012-04-02 04:24:58

标签: php time

对于略带误导性的标题感到抱歉,我不确定如何说出来。

基本上,我正在制作一个时钟输入系统。

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

在特定时间段内显示所有clockins之后,脚本会累计两个时间戳之间的所有差异。

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

2 个答案:

答案 0 :(得分:1)

从更详细的输出中使用此功能

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;你想要显示时间的地方。

答案 1 :(得分:0)

<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>

是的,它有效。