我有這個功能顯示友好日期TIMEAGO與UNIX時間戳和日期格式:PHP TIMEAGO友好的日期不行
function friendlyDate($timestamp, $formats = null)
{
$_DATE_FORMAT = array(
'DAY' => 'DAY',
'DAY_HOUR' => 'DAY_HOUR',
'HOUR' => 'HOUR',
'HOUR_MINUTE' => 'HOUR_MINUTE',
'MINUTE' => 'MINUTE',
'MINUTE_SECOND' => 'MINUTE_SECOND',
'SECOND' => 'SECOND',
);
if ($formats == null) {
$formats = $_DATE_FORMAT;
}
$seconds = time() - $timestamp;
$minutes = floor($seconds/60);
$hours = floor($minutes/60);
$days = floor($hours/24);
if ($days > 0 && $days<=3) {
$diffFormat = 'DAY';
} else if($days > 3){
return date('Y-m-d',$timestamp);
} else {
$diffFormat = ($hours > 0) ? 'HOUR' : 'MINUTE';
if ($diffFormat == 'HOUR') {
$diffFormat .= ($minutes > 0 && ($minutes - $hours * 60) > 0) ? '_MINUTE' : '';
} else {
$diffFormat = (($seconds - $minutes * 60) > 0 && $minutes > 0)
? $diffFormat.'_SECOND' : 'SECOND';
}
}
$dateDiff = null;
switch ($diffFormat) {
case 'DAY':
$dateDiff = sprintf($formats[$diffFormat], $days);
break;
case 'DAY_HOUR':
$dateDiff = sprintf($formats[$diffFormat], $days, $hours - $days * 60);
break;
case 'HOUR':
$dateDiff = sprintf($formats[$diffFormat], $hours);
break;
case 'HOUR_MINUTE':
$dateDiff = sprintf($formats[$diffFormat], $hours, $minutes - $hours * 60);
break;
case 'MINUTE':
$dateDiff = sprintf($formats[$diffFormat], $minutes);
break;
case 'MINUTE_SECOND':
$dateDiff = sprintf($formats[$diffFormat], $minutes, $seconds - $minutes * 60);
break;
case 'SECOND':
$dateDiff = sprintf($formats[$diffFormat], $seconds);
break;
}
return $dateDiff;
}
我echo
friendlyDate在我的網頁是這樣的:
echo friendlyDate(1436613754,'');
但我看到這個結果:SECOND
我能做些什麼來解決這個問題,我想顯示日期和時間。
只是爲了確保它的清楚,你能[編輯]的問題,包括預期輸出? – IMSoP
@IMSoP:輸出是:'SECOND'。我需要*秒前 – Perspolis
就像我說的那樣,將它編入問題中,我的意思是所示示例的確切答案。 – IMSoP