一、timetosec函數
timetosec函數用於將一段時間字元串轉換成秒數。
function timetosec($time) { $hours = substr($time, 0, -4); $minutes = substr($time, -4, 2); $seconds = substr($time, -2); return $hours * 3600 + $minutes * 60 + $seconds; }
該函數可以處理時間字元串,如”01:12:30″,並將其轉換為相應的秒數。
例如:
echo timetosec("01:12:30"); // 4350
二、timetoseconds
timetoseconds函數用於將一個時間段轉換成秒數,例如”5minutes”或”2hours30minutes”。
function timetoseconds($time) { preg_match_all('/(\d+)\s*(minutes|minute|hours|hour)/', $time, $matches); $seconds = 0; foreach ($matches[1] as $key => $value) { if ($matches[2][$key] == 'hour' || $matches[2][$key] == 'hours') { $seconds += $value * 3600; } if ($matches[2][$key] == 'minute' || $matches[2][$key] == 'minutes') { $seconds += $value * 60; } } return $seconds; }
該函數可以將一個時間段字元串,如”5minutes”或”2hours30minutes”,轉換成相應的秒數。
例如:
echo timetoseconds("5minutes"); // 300 echo timetoseconds("2hours30minutes"); // 9000
三、timetosecond
timetosecond函數是timetoseconds的簡短版,只能接受”minutes”或”hours”兩種時間單位。
function timetosecond($time) { if (strpos($time, "minutes") !== false) { return intval($time) * 60; } else if (strpos($time, "hours") !== false) { return intval($time) * 3600; } }
該函數可以將一個時間段字元串,如”5minutes”或”2hours”,轉換成相應的秒數。
例如:
echo timetosecond("5minutes"); // 300 echo timetosecond("2hours"); // 7200
四、timetosec(133000)
timetosec函數可以將一個整數時間轉換成相應的時間字元串。
function timetosec($time) { $hours = floor($time / 3600); $time %= 3600; $minutes = floor($time / 60); $seconds = $time % 60; return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); }
該函數可以將一個整數時間,如”133000″,轉換成相應的時間字元串,例如”01:13:20″。
例如:
echo timetosec(133000); // 01:13:20
五、timetosec(103000)
timetosec函數可以將一個整數時間轉換成相應的時間字元串。
function timetosec($time) { $hours = floor($time / 3600); $time %= 3600; $minutes = floor($time / 60); $seconds = $time % 60; return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); }
該函數可以將一個整數時間,如”103000″,轉換成相應的時間字元串,例如”00:57:10″。
例如:
echo timetosec(103000); // 00:57:10
六、time to sec
time to sec函數可以將一個時間字元串,如”6 minutes 30 seconds”,轉換成相應的秒數。
function time_to_sec($time) { $parts = array_reverse(explode(' ', $time)); $seconds = 0; $multipliers = array( 'hour' => 3600, 'hours' => 3600, 'minute' => 60, 'minutes' => 60, 'second' => 1, 'seconds' => 1 ); foreach ($parts as $part) { if (isset($multipliers[$part])) { $seconds += $multipliers[$part] * array_pop($parts); } } return $seconds; }
該函數可以將一個時間字元串,如”6 minutes 30 seconds”,轉換成相應的秒數。
例如:
echo time_to_sec("6 minutes 30 seconds"); // 390 echo time_to_sec("2 hours 30 minutes 45 seconds"); // 9045
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309777.html