日時の切り上げ/切捨て/四捨五入
ceil、floor、roundを使って時間の切り上げ、切捨て、四捨五入
PHP
/**
*
* 日時の切り上げ/切捨て/四捨五入
*
* @param string $date
* @param integer $place 1|10|100|1000|10000|100000
* @param string $math ceil|floor|round
* @return string
*/
function getDateRound($date, $place, $math)
{
list($Y, $m, $d, $H, $i, $s) = explode("-", date('Y-m-d-H-i-s', strtotime($date)));
// 秒(1で1の位、10で秒全体)
if ($place == 1) {
$s = $math($s * 0.1) * 10;
} elseif ($place > 1) {
$s = $math($s * 0.01) * 100;
if ($s > 60) {
$s = 60;
}
}
if ($place > 10) {
$date = date('Y-m-d-H-i-s', mktime($H, $i, $s, $m, $d, $Y));
list($Y, $m, $d, $H, $i, $s) = explode("-", $date);
}
// 分(100で1の位、1000で分全体)
if ($place == 100) {
$i = $math($i * 0.1) * 10;
} elseif ($place > 100) {
$i = $math($i * 0.01) * 100;
if ($i > 60) {
$i = 60;
}
}
if ($place > 1000) {
$date = date('Y-m-d-H-i-s', mktime($H, $i, $s, $m, $d, $Y));
list($Y, $m, $d, $H, $i, $s) = explode("-", $date);
}
// 時間(10000で1の位、100000で時間全体)
if ($place == 10000) {
$H = $math($H * 0.1) * 10;
} elseif ($place > 10000) {
$H = $math($H * 0.01) * 100;
if ($H > 24) {
$H = 24;
}
}
return date('Y-m-d H:i:s', mktime($H, $i, $s, $m, $d, $Y));
}
計算結果
値($place) | ceil | floor | round |
---|---|---|---|
1 | 2024-11-15 10:25:00 | 2024-11-15 10:24:50 | 2024-11-15 10:24:50 |
10 | 2024-11-15 10:25:00 | 2024-11-15 10:24:00 | 2024-11-15 10:25:00 |
100 | 2024-11-15 10:30:00 | 2024-11-15 10:20:00 | 2024-11-15 10:30:00 |
1000 | 2024-11-15 11:00:00 | 2024-11-15 10:00:00 | 2024-11-15 10:00:00 |
10000 | 2024-11-15 20:00:00 | 2024-11-15 10:00:00 | 2024-11-15 10:00:00 |
100000 | 2024-11-16 00:00:00 | 2024-11-15 00:00:00 | 2024-11-15 00:00:00 |