3 回答
TA贡献1836条经验 获得超5个赞
您的全部功能将是这样的...
function roundToQuarterHour($timestring) {
$minutes = date('i', strtotime($timestring));
return $minutes - ($minutes % 15);
}
TA贡献1890条经验 获得超9个赞
$seconds = time();
$rounded_seconds = round($seconds / (15 * 60)) * (15 * 60);
echo "Original: " . date('H:i', $seconds) . "\n";
echo "Rounded: " . date('H:i', $rounded_seconds) . "\n";
本示例获取当前时间并将其四舍五入到最接近的四分之一,并打印原始时间和四舍五入时间。
PS:如果你想详谈下来替换round()用floor()。
TA贡献1810条经验 获得超4个赞
$now = getdate();
$minutes = $now['minutes'] - $now['minutes']%15;
//Can add this to go to the nearest 15min interval (up or down)
$rmin = $now['minutes']%15;
if ($rmin > 7){
$minutes = $now['minutes'] + (15-$rmin);
}else{
$minutes = $now['minutes'] - $rmin;
}
$rounded = $now['hours'].":".$minutes;
echo $rounded;
- 3 回答
- 0 关注
- 324 浏览
添加回答
举报