我一直在努力工作一个小时左右,并且空白了.基本上我需要花费当前时间,增加30分钟,然后向下舍入到接下来的15分钟.
例子:
我的PHP生疏了,我一直在混淆日期添加和圆形方法试图让它工作,我知道它很简单 - 刚刚用完了想法!
谢谢
我还要添加一个解决方案:
<?php
date_default_timezone_set('America/Los_Angeles');
$times = array();
$times[] = strtotime('00:07');
$times[] = strtotime('04:21');
$times[] = strtotime('20:00');
$times[] = strtotime('20:10');
$times[] = strtotime('20:16');
$times[] = strtotime('20:35');
$times[] = strtotime('23:15');
foreach($times as $time) {
echo date('m-d-Y H:i', $time) . ' becomes ' . date('m-d-Y H:i:s', roundToNearestInterval($time)) . "<br />\n";
}
function roundToNearestInterval($timestamp)
{
$timestamp += 60 * 30;
list($m, $d, $y, $h, $i, $s) = explode(' ', date('m d Y H i s', $timestamp));
if ($s != 0) $s = 0;
if ($i < 15) {
$i = 15;
} else if ($i < 30) {
$i = 30;
} else if ($i < 45) {
$i = 45;
} else if ($i < 60) {
$i = 0;
$h++;
}
return mktime($h, $i, $s, $m, $d, $y);
}
Run Code Online (Sandbox Code Playgroud)
产量:
03-01-2012 00:07 becomes 03-01-2012 00:45:00
03-01-2012 04:21 becomes 03-01-2012 05:00:00
03-01-2012 20:00 becomes 03-01-2012 20:45:00
03-01-2012 20:10 becomes 03-01-2012 20:45:00
03-01-2012 20:16 becomes 03-01-2012 21:00:00
03-01-2012 20:35 becomes 03-01-2012 21:15:00
03-01-2012 23:15 becomes 03-02-2012 00:00:00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1723 次 |
| 最近记录: |