PHP时间格式 [英] Php Time Format

查看:60
本文介绍了PHP时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个获取列平均值的 mysql 查询(列数据类型为时间").例如,列值是:00:00:55、00:00:59、00:01:03

I have here a mysql query that get the average of the column(the column data type is 'time'). The column values for example are: 00:00:55, 00:00:59, 00:01:03

SELECT AVG(TIME_TO_SEC(column_name)) FROM table_name)AS average_result

在我的 PHP 中,我以这种方式格式化结果:

In my Php I formatted the result this way:

<?php foreach($display_average as $da){
     echo date("H:i:s", ($da["average_result"]));
}
?>

输出:08:00:59 而不是 00:00:59,为什么以 08 开头?还是我错过了什么?谢谢!

Outputs: 08:00:59 instead of 00:00:59, Why does this starts with 08? Or did I miss something? Thanks!

推荐答案

PHP 的日期/时间函数和 MySQL 的日期/时间数据类型都处理挂钟时间戳,而不是持续时间;即 00:00:55 表示 午夜后五十五秒.这不是你想要的;您不能处理持续时间超过 23 小时 59 分 59 秒,因为您使用的数据类型和函数正在处理时钟时间,不能超过这些值.

Both PHP's date/time functions and MySQL's date/time data types handle wall clock timestamps, not durations; i.e. 00:00:55 means fifty-five seconds past midnight. This is not what you want; you couldn't handle durations longer than 23 hours, 59 minutes, 59 seconds, because the data types and functions you're using are handling clock time, which cannot exceed these values.

您的具体问题源于时区设置.您更大的问题是您需要存储表示经过的秒数或分钟数的简单整数值;不是时间戳.要将其格式化为 PHP 中人类可读的字符串,您可以使用 DateInterval.

Your specific issue stems from timezone settings. Your larger issue is that you need to store simple integer values expressing elapsed seconds or minutes; not timestamps. To format that into a human readable string in PHP you can use the DateInterval class.

这篇关于PHP时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆