53
A
回答
91
无需任何explode
:
$str_time = "23:12:95";
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
如果你不想使用正则表达式:
$str_time = "2:50";
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
4
伪代码:
split it by colon
seconds = 3600 * HH + 60 * MM + SS
0
<?php
$time = '21:32:32';
$seconds = 0;
$parts = explode(':', $time);
if (count($parts) > 2) {
$seconds += $parts[0] * 3600;
}
$seconds += $parts[1] * 60;
$seconds += $parts[2];
-3
// HH:MM:SS or MM:SS
echo substr($time , "-2"); // returns last 2 chars : SS
5
试试这个:
$time = "21:30:10";
$timeArr = array_reverse(explode(":", $time));
$seconds = 0;
foreach ($timeArr as $key => $value)
{
if ($key > 2) break;
$seconds += pow(60, $key) * $value;
}
echo $seconds;
-1
$time="12:10:05"; //your time
echo strtotime("0000-00-00 $time")-strtotime("0000-00-00 00:00:00");
82
我觉得最简单的方法是使用strtotime()
功能:
$time = '21:30:10';
$seconds = strtotime("1970-01-01 $time UTC");
echo $seconds;
// same with objects (for php5.3+)
$time = '21:30:10';
$dt = new DateTime("1970-01-01 $time", new DateTimeZone('UTC'));
$seconds = (int)$dt->getTimestamp();
echo $seconds;
功能date_parse()
也可用于解析日期和时间:
$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
如果将解析格式MM:SS
与strtotime()
或date_parse()
它会失败(date_parse()
在strtotime()
使用和DateTime
),因为当你输入格式,如xx:yy
解析器假定它是HH:MM
而不是MM:SS
。我建议检查格式,并且如果您只有MM:SS
,则需要输入00:
。
demostrtotime()
demodate_parse()
如果你有时间超过24,那么你可以使用下面的函数(它会为MM:SS
和HH:MM:SS
格式工作):
function TimeToSec($time) {
$sec = 0;
foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
return $sec;
}
1
简单
function timeToSeconds($time)
{
$timeExploded = explode(':', $time);
if (isset($timeExploded[2])) {
return $timeExploded[0] * 3600 + $timeExploded[1] * 60 + $timeExploded[2];
}
return $timeExploded[0] * 3600 + $timeExploded[1] * 60;
}
1
$time = 00:06:00;
$timeInSeconds = strtotime($time) - strtotime('TODAY');
0
可以使用strtotime
函数从today 00:00:00
返回的秒数。
$seconds= strtotime($time) - strtotime('00:00:00');
相关问题
- 1. 程序将时间以秒为单位转换为hh:mm:ss格式
- 2. 将hh:mm:ss转换为秒
- 3. 转换器 - >将日期时间转换为HH:mm:ss格式
- 4. 将小时,分钟和秒转换为HH:MM:SS格式
- 5. 使用moment.js将秒转换为hh:mm:ss格式
- 6. 将yyyy-mm-dd hh:mm:ss格式转换为时间前
- 7. 在teradata中将秒转换为HH:MM:SS
- 8. SQLite将毫秒转换为HH:MM:SS
- 9. 将时间转换为秒+毫秒秒
- 10. 将毫秒转换为小时格式
- 11. 将时间(hh:mm:ss)转换为R
- 12. 转换HH:MM:SS时间格式为在谷歌电子表格
- 13. 如何将秒数格式化为HH:MM:SS?
- 14. 有没有办法将时间从“hh:mm:ss”格式转换为“hh:mm AM/PM”格式?
- 15. 如何转换格式为“HH:mm:ss”的秒数
- 16. 如何将时间格式为'00:04:13.67'转换为秒?
- 17. 如何使用瞬间js将“hh:mm:ss”时间格式转换为小时,分钟,秒?
- 18. 将ISO 8601格式的时间段转换为ticks /毫秒
- 19. 将时间格式mm.ss.dd转换为MS Excel中的秒数
- 20. 将秒转换为可读格式时间
- 21. 将秒转换为格式化时间,如12:59?
- 22. MySQL将单元值从秒转换为时间格式
- 23. 如何将时间以秒为单位转换为标准时间格式?
- 24. 转换格式为HH:MM AM/PM至HH:MM:SS
- 25. 有没有一个unix程序将各种HH:MM:SS格式转换为秒?
- 26. Javascript - 将日期格式转换为秒
- 27. PHP将秒转换为美分格式
- 28. 如何将“'n'min'm'secs”转换为秒格式
- 29. 将系统日期时间转换为特定格式。例如:(yyyyMMdd hh:MM:ss)
- 30. Qt的C++秒转换到格式化字符串(HH:MM:SS)
这是PHP吗? 8O – Ryan 2011-01-29 00:06:43
正如他写的,它是伪代码。 “伪装代码”来解释如何做一些事情。 – simme 2011-01-29 00:10:11