使用PHP将时间戳四舍五入到最近的时间
例如,如果要创建“四舍五入”的时间戳,请精确到15分钟,以此为参考:
function roundTime($amount, $units = 'm', $time = null)
{
if (is_null($time)) {
$time = time();
}
if ($amount == 0) {
$amount = 1;
}
switch ($units){
case 'm':
$roundBy = 60 * $amount;
break;
case 'h':
$time = $time + 60 * 60 * $amount;
$roundBy = 60 * 60 * $amount;
break;
case 'd':
$time = $time + 60 * 60 * 24 * $amount;
$roundBy = 60 * 60 * 24 * $amount;
break;
}
return round($time / $roundBy) * $roundBy;
}
echo "\n";
echo date('r', roundTime(15, 'm'));
echo "\n";
echo date('r', roundTime(2, 'h'));
echo "\n";
echo date('r', roundTime(3, 'd'));
echo "\n";
echo date('r', roundTime(-3, 'd'));
echo "\n";热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短