从方向api输出我有一个持续时间,它将使用户从a到b。目前是几分钟,但如果用户旅程需要3小时20分钟,则输出200分钟。
我希望它能够超过60分钟。然后除以60并加上余数给出
3小时20分钟。
我们如何做到这一点。
非凡
答案 0 :(得分:21)
使用php> = 5.3.0你可以这样做:
$dt = new DateTime();
$dt->add(new DateInterval('PT200M'));
$interval = $dt->diff(new DateTime());
echo $interval->format('%Hh %Im %Ss');
输出(在我的语言环境中):02h 40m 00s
答案 1 :(得分:9)
function getNiceDuration($durationInSeconds) {
$duration = '';
$days = floor($durationInSeconds / 86400);
$durationInSeconds -= $days * 86400;
$hours = floor($durationInSeconds / 3600);
$durationInSeconds -= $hours * 3600;
$minutes = floor($durationInSeconds / 60);
$seconds = $durationInSeconds - $minutes * 60;
if($days > 0) {
$duration .= $days . ' days';
}
if($hours > 0) {
$duration .= ' ' . $hours . ' hours';
}
if($minutes > 0) {
$duration .= ' ' . $minutes . ' minutes';
}
if($seconds > 0) {
$duration .= ' ' . $seconds . ' seconds';
}
return $duration;
}
答案 2 :(得分:5)
如果您有持续时间(以秒为单位),可以使用PHP gmdate()
函数格式化它:
echo gmdate("H:i:s", $seconds);
(注意:工作时间长达24小时)
答案 3 :(得分:3)
$minutes = 200;
if ($minutes >= 60)
{
$hours = (int)($minutes / 60);
$minutes = $minutes % 60;
}
答案 4 :(得分:1)
<?php
$time = 200; // minutes
if ($time > 60) {
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;
}
echo "$hours hours $minutes minutes";
?>
使用模数除法:)
答案 5 :(得分:0)
function format_minutes($value)
{
$hours = intval($value / 60);
$minutes = $value % 60;
if ($hours != 0) {
$str = $hours . ' hour';
// Handle pluralisation.
if (abs($hours) != 1) {
$str .= 's';
}
}
// Always show minutes if there are no hours.
if ($minutes != 0 || $hours == 0) {
$str .= ' ' . $minutes . ' minute';
// Handle pluralisation.
if (abs($minutes) != 1) {
$str .= 's';
}
}
// There will be a leading space if hours is zero.
return trim($str);
}
答案 6 :(得分:0)
感谢您的功能。我需要改变它以使其更好地工作: 会议记录超过60分钟:
$seconds = $secs % 60;
$hours = floor($secs / 3600);
$secs = $secs - $hours*3600;
$minutes = floor($secs / 60);
最佳
答案 7 :(得分:0)
您可以使用DateInterval->format
。
$rawInterval = 'PT200M';
$formattedInterval = (new DateInterval($rawInterval))->format('%Hh %Im %Ss');
输出为:02h 40m 00s
。
答案 8 :(得分:0)
对于那些需要它的人...时间段-PT:
<?php
function pttime($time, $format)
{
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
//is PT
if ($format == 'PT') {
//full hour
if (($hours > 0) && ($minutes == 0)) {
$time_result = 'PT' . $hours . 'H';
}
//hour and minutes
if (($hours > 0) && ($minutes <> 0)) {
$time_result = 'PT' . $hours . 'H' . $minutes . 'M';
}
//just minutes
if ($hours == 0) {
$time_result = 'PT' . $minutes . 'M';
}
}
//it's not PT
else {
$time_result = sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes);
}
return $time_result;
}
//input in minutes and its outputs
echo pttime(155,'PT'); //output -> PT2H35M
echo pttime(52,'PT'); //output -> PT52M
echo pttime(60,'PT'); //output -> PT1H
echo pttime(60,''); //output -> 01:00
答案 9 :(得分:-2)
一个简单的持续时间格式化时间示例(持续时间以秒为单位,输出格式为[##:] ##:##):
public function format_duration($secs, $delimiter = ':')
{
$seconds = $secs % 60;
$minutes = floor($secs / 60);
$hours = floor($secs / 3600);
$seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
$minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT).$delimiter;
$hours = ($hours > 0) ? str_pad($hours, 2, "0", STR_PAD_LEFT).$delimiter : '';
return "$hours$minutes$seconds";
}