一. php把秒转换成天数,小时数和分钟格式时间简单写法
function secstostr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
$r.=$secs.' second';if($secs<>1){$r.='s';}
return $r;
}
二. php把小于多少秒转换成天数,小时数和分钟格式时间完整写法
/**
* 时间友好型提示风格化(即微博中的xxx小时前、昨天等等)
*
* 即微博中的 xxx 小时前、昨天等等, 时间超过 $time_limit 后返回按 out_format 的设定风格化时间戳
*
* @param int
* @param int
* @param string
* @param array
* @param int
* @return string
*/
function date_friendly($timestamp, $time_limit = 604800, $out_format = 'y-m-d h:i', $formats = null, $time_now = null)
{
if (get_setting('time_style') == 'n')
{
return date($out_format, $timestamp);
}
if (!$timestamp)
{
return false;
}
if ($formats == null)
{
$formats = array('year' => aws_app::lang()->_t('%s 年前'), 'month' => aws_app::lang()->_t('%s 月前'), 'day' => aws_app::lang()->_t('%s 天前'), 'hour' => aws_app::lang()->_t('%s 小时前'), 'minute' => aws_app::lang()->_t('%s 分钟前'), 'second' => aws_app::lang()->_t('%s 秒前'));
}
$time_now = $time_now == null ? time() : $time_now;
$seconds = $time_now - $timestamp;
if ($seconds == 0)
{
$seconds = 1;
}
if (!$time_limit or $seconds > $time_limit)
{
return date($out_format, $timestamp);
}
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$days = floor($hours / 24);
$months = floor($days / 30);
$years = floor($months / 12);
if ($years > 0)
{
$diffformat = 'year';
}
else
{
if ($months > 0)
{
$diffformat = 'month';
}
else
{
if ($days > 0)
{
$diffformat = 'day';
}
else
{
if ($hours > 0)
{
$diffformat = 'hour';
}
else
{
$diffformat = ($minutes > 0) ? 'minute' : 'second';
}
}
}
}
$datediff = null;
switch ($diffformat)
{
case 'year' :
$datediff = sprintf($formats[$diffformat], $years);
break;
case 'month' :
$datediff = sprintf($formats[$diffformat], $months);
break;
case 'day' :
$datediff = sprintf($formats[$diffformat], $days);
break;
case 'hour' :
$datediff = sprintf($formats[$diffformat], $hours);
break;
case 'minute' :
$datediff = sprintf($formats[$diffformat], $minutes);
break;
case 'second' :
$datediff = sprintf($formats[$diffformat], $seconds);
break;
}
return $datediff;
}
相关文章:
php把秒转换成天数,小时数和分钟
js中的时间转换—毫秒转换成日期时间的示例代码