php 将 Unix 时间戳转换为小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5904612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Unix timestamp to hours
提问by name_masked
All,
全部,
I have a database field which actually computes the time spent from the time the quiz started to the current time. This was done at some point of time (recorded as current time) and now I have an Unixtimestamp value for it
我有一个数据库字段,它实际计算从测验开始到当前时间花费的时间。这是在某个时间点完成的(记录为当前时间),现在我有一个 Unixtimestamp 值
i.e. Suppose the start time was 5/5/2011 1pm and the current time was populated at 5/5/2011 2pm. So the difference is calculated and stored as an Unix timestamp.
即假设开始时间是 5/5/2011 1pm,当前时间填充在 5/5/2011 2pm。因此,差异被计算并存储为 Unix 时间戳。
Now I don't know when this was actually done and now I have to revert back to hours spent taking the quiz. So here a Unix timestamp needs to be converted back to hours, in this case return 1 hour. Can anyone please help me understand how to do this?
现在我不知道这实际上是什么时候完成的,现在我必须回到参加测验所花费的时间。所以这里需要将 Unix 时间戳转换回小时,在这种情况下返回 1 小时。谁能帮我理解如何做到这一点?
回答by Riccardo Galli
You have the seconds, so just do something like
你有时间,所以就做类似的事情
SELECT secondsField / 3600 as 'hours' FROM tableName
回答by Zorayr
Here is an example of finding the difference between two timestamps in days.
这是一个以天为单位查找两个时间戳之间差异的示例。
//Get current timestamp.
$current_time = time();
//Convert user's create time to timestamp.
$create_time = strtotime('2011-09-01 22:12:55');
echo "Current Date \t".date('Y-m-d H:i:s', $current_time)."\n";
echo "Create Date \t".date('Y-m-d H:i:s', $create_time)."\n";
echo "Current Time \t ".$current_time." \n";
echo "Create Time \t ".$create_time." \n";
$time_diff = $current_time - $create_time;
$day_diff = $time_diff / (3600 * 24);
echo "Difference \t ".($day_diff)."\n";
echo "Quote Index \t ".intval(floor($day_diff))."\n";
echo "Func Calc \t ".get_passed_days($create_time)."\n";
function get_passed_days($create_time)
{
return intval(floor((time() - $create_time) / 86400));
}
To convert to hours, instead of 86400 put 3600 instead.
要转换为小时,请用 3600 代替 86400。
Hope this helps.
希望这可以帮助。