显示时间在24小时...如果不是过去了 30 分钟,我可以只显示小时吗?



有没有一种方法可以只在时间不超过30分钟的情况下显示时间,而在时间超过30分钟时显示:30?

http://new.clairvoyant.co/details/?Pin=4378这是我的页面。在时间表中,你可以看到当区块非常短时,要显示其中的时间段真的很困难。

如果开始和结束时间只有几个小时,我想做的是显示下午12点到4点这样的格式。如果其中一个是30分钟,则显示下午12点至下午4点30分。

只需格式化strttotime就可以做到这一点吗?输入变量以24小时(24:00:00)为单位。结束输出的格式为12小时。

$Start12 = strtotime($Shift['Start']);
$Stop12 = strtotime($Shift['Stop']);

并输出如下:

<span class='c'>".date('g:ia',$Start12)." to ".date('g:ia',$Stop12)."</span>

假设分钟数为30:

function printDate30($date){
if(date('i',$date) == 30){
return date('g:ia',$date);
}else{
return date('ga',$date);
}
}

并使用

echo "<span class='c'>".printDate30($Start12)." to ".printDate30($Stop12)."</span>";

演示:http://codepad.org/c2AhcELC

这里有一个很好的例子,不过我还没有检查语法:

// Create a new object from your start time
$firstDateTime = new DateTime( strtotime( $Shift['Start'] ) );
$firstDateTimeFormatter = 'H'; // Set the object to be default formatted for the hour only
// Repeat, blah...
$secondDateTime = new DateTime( strtotime( $Shift['Stop'] ) );
$secondDateTimeFormatter = 'H';
// If the minutes for this object are not 00 then we want to format the time
// differently, these two checks do that
if( $firstDateTime->format('i') != '00' )
$firstDateTimeFormatter = 'H:m';
if( $secondDateTime->format('i') != '00' )
$secondDateTimeFormatter = 'H:m';
// See the results
echo $firstDateTime->format( $firstDateTimeFormatter );
echo $secondDateTime->format( $secondDateTimeFormatter );

// Put this into a convenient function to use
function formatTime( $time )
{
$dt = new DateTime( strtotime( $time ) );
return ( $dt->format('i') != '00' ? $dt->format('H:i') : $dt->format('H') );
}

相关内容

最新更新