将 Unix 时间戳转换为小时

发布于 2024-11-05 02:18:57 字数 265 浏览 1 评论 0原文

所有,

我有一个数据库字段,它实际上计算从测验开始到当前时间所花费的时间。这是在某个时间点完成的(记录为当前时间),现在我有一个 Unixtimestamp 值

,即假设开始时间为 5/5/2011 1pm,当前时间填充为 5/5/2011 2pm。因此计算差异并将其存储为 Unix 时间戳。

现在我不知道这实际上是什么时候完成的,现在我必须回到花在参加测验上的时间。因此,这里需要将 Unix 时间戳转换回小时,在本例中返回 1 小时。谁能帮我理解如何做到这一点?

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2024-11-12 02:18:57

你有时间,所以就做类似的事情

SELECT secondsField / 3600 as 'hours' FROM tableName

You have the seconds, so just do something like

SELECT secondsField / 3600 as 'hours' FROM tableName
段念尘 2024-11-12 02:18:57

下面是查找两个时间戳之间的天数差异的示例。

//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));
}

要转换为小时,请将 86400 改为 3600。

希望这有帮助。

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.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文