本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑
php 秒怎么转换成分?
php 自动把秒转换成分,时,天,周,月,年...
<?php
class Calendar
{
const TIME_MINUTE = 60;
const TIME_HOURS = 3600;
const TIME_DAY = 3600 * 24;
const TIME_WEEK = 3600 * 24 * 7;
const TIME_MONTH = 3600 * 24 * 30;
const TIME_YEAR = 3600 * 24 * 365;
const TIME_YEAR_INTERCALARY = 3600 * 24 * 366;
const TIME_NAMES = [
0 => '秒',
self::TIME_MINUTE => '分钟',
self::TIME_HOURS => '小时',
self::TIME_DAY => '天',
self::TIME_WEEK => '周',
self::TIME_MONTH => '月',
self::TIME_YEAR => '年',
];
public static function formatSecond($second = 0, $unitString = self::TIME_NAMES)
{
ksort($unitString);
if ($second < self::TIME_MINUTE) return $second . $unitString[0];
krsort($unitString);
foreach ($unitString as $time => $unitName) {
if ($time <= 0) continue;
$result = floor($second / $time);
if ($result >= 1) return $result . $unitName;
}
return null;
}
}