私の最高の仕事からは遠いですが、ここでは文字列またはunixのタイムスタンプを扱うためのいくつかの関数があります。
/**
* Takes a timestamp like "2016-10-01 17:59:01" and returns "2016-10-01 18:00"
* Note: assumes timestamp is in UTC
*
* @param $timestampString - a valid string which will be converted to unix with time()
* @param int $mins - interval to round to (ex: 15, 30, 60);
* @param string $format - the format to return the timestamp default is Y-m-d H:i
* @return bool|string
*/
function roundTimeString($timestampString, $mins = 30, $format="Y-m-d H:i") {
return gmdate($format, roundTimeUnix(time($timestampString), $mins));
}
/**
* Rounds the time to the nearest minute interval, example: 15 would round times to 0, 15, 30,45
* if $mins = 60, 1:00, 2:00
* @param $unixTimestamp
* @param int $mins
* @return mixed
*/
function roundTimeUnix($unixTimestamp, $mins = 30) {
$roundSecs = $mins*60;
$offset = $unixTimestamp % $roundSecs;
$prev = $unixTimestamp - $offset;
if($offset > $roundSecs/2) {
return $prev + $roundSecs;
}
return $prev;
}
http://php.net/manual/en/function.strtotime.phpを手動で機能させてみると便利です。タイムスタンプから任意の時間を追加したり削除したりするのは非常に簡単です。 – Kamil
これは時間と日付とは何が関係していますか?本当に四捨五入されていない簡単な数学 – miki