我有日期时间:
2014-08-05 13:27:00
2014-08-05 13:29:00
2014-08-05 13:36:00
2014-08-05 13:38:00
我该如何修改DateTime::modify()
以设置为
2014-08-05 13:15:00
2014-08-05 13:15:00
2014-08-05 13:30:00
2014-08-05 13:30:00
(因此,请在最后15分钟标记。)
当您要求它使用时DateTime::modify()
,您可以这样做:
function roundToLastQuarterHour($date_string) {
$date = new DateTime($date_string);
// Remove any seconds, we don't need them
$seconds = $date->format('s');
$date->modify('-' . $seconds . ' seconds');
// Store the original number of minutes on the datetime
$original_minutes = $date->format('i');
// Calculate how many minutes past the last quarter hour
$remaining_minutes = $original_minutes - ($original_minutes - ($original_minutes % 15));
// Modify the minutes, remove the number of minutes past the last quarter of an hour
$date->modify('-' . $remaining_minutes . ' minutes');
return $date;
}
roundToLastQuarterHour('2014-08-05 13:27:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:29:00')
// 2014-08-05 13:15:00
roundToLastQuarterHour('2014-08-05 13:36:00')
// 2014-08-05 13:30:00
roundToLastQuarterHour('2014-08-05 13:38:00')
// 2014-08-05 13:30:00
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句