将秒转换为人类可读的持续时间

发布于 2024-12-17 11:02:26 字数 649 浏览 1 评论 0 原文

我如何最好地将 90060(秒)转换为字符串“25h 1m”?

目前我正在 SQL 中执行此操作:

SELECT 
  IF(
    HOUR(
      sec_to_time(
        sum(time_to_sec(task_records.time_spent))
      )
    ) > 0, 
    CONCAT(
      HOUR(sec_to_time(sum(time_to_sec(task_records.time_spent)))), 
      'h ', 
      MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
      'm'
    ), 
    CONCAT(
      MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
      'm'
    )
  ) as time
FROM myTable;

但我不确定这是最方便的方法:-)

我愿意接受在 SQL 中(与我已经做的不同)或 PHP 中执行此操作的建议。

编辑:

所需字符串的示例:“5m”、“40m”、“1h 35m”、“45h”“46h 12m”。

How would I best convert 90060 (seconds) to a string of "25h 1m"?

Currently I'm doing this in SQL:

SELECT 
  IF(
    HOUR(
      sec_to_time(
        sum(time_to_sec(task_records.time_spent))
      )
    ) > 0, 
    CONCAT(
      HOUR(sec_to_time(sum(time_to_sec(task_records.time_spent)))), 
      'h ', 
      MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
      'm'
    ), 
    CONCAT(
      MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
      'm'
    )
  ) as time
FROM myTable;

But I'm not sure it's the most convenient method :-)

I'm open to suggestions on doing this both in SQL (differently than I'm already doing) or in PHP.

EDIT:

Examples of desired strings: "5m", "40m", "1h 35m", "45h" "46h 12m".

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

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

发布评论

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

评论(4

白芷 2024-12-24 11:02:27

您可以使用预定义函数 sec_to_time()

sec_to_time(number_of_seconds)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time

you can use predefined function sec_to_time()

sec_to_time(number_of_seconds)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time

人心善变 2024-12-24 11:02:27

尝试这个技巧(在 MySQL 上)

select date_format("1970-01-01 00:00:00" + interval <value_here> second, "%H:%i:%S")

SEC_TO_TIME 有时会产生错误的值

Try out this trick (on MySQL)

select date_format("1970-01-01 00:00:00" + interval <value_here> second, "%H:%i:%S")

SEC_TO_TIME sometime produces wrong values

勿忘初心 2024-12-24 11:02:27

试试这个:(输入你的纯秒时间)

var hours = Math.floor(input/3600);
var minutes = Math.floor((input-hours*3600)/60);
var seconds = input-(minutes*60)-(hours*3600);
function convertTime(){
return hours":"minutes":"seconds;
}

try this: (input your pure seconds time)

var hours = Math.floor(input/3600);
var minutes = Math.floor((input-hours*3600)/60);
var seconds = input-(minutes*60)-(hours*3600);
function convertTime(){
return hours":"minutes":"seconds;
}
伴梦长久 2024-12-24 11:02:26
TIME_FORMAT(SEC_TO_TIME(task_records.time_spent),'%Hh %im')

文档是你的朋友:


根据评论:

DROP FUNCTION IF EXISTS GET_HOUR_MINUTES;
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
  RETURNS VARCHAR(16)

  BEGIN
  DECLARE result VARCHAR(16);
  IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
  ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
  RETURN result;
  END

DELIMETER ;

用法:

SELECT GET_HOUR_MINUTES(task_records.time_spent) FROM table
TIME_FORMAT(SEC_TO_TIME(task_records.time_spent),'%Hh %im')

Documentation is your friend:


According to comment:

DROP FUNCTION IF EXISTS GET_HOUR_MINUTES;
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
  RETURNS VARCHAR(16)

  BEGIN
  DECLARE result VARCHAR(16);
  IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
  ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
  RETURN result;
  END

DELIMETER ;

Usage:

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