整整一分钟到最近的四分之一小时

我需要在PHP中将时间缩短到最接近的四分之一小时。 时间正在从date时间列的MySQL数据库中拉出,并格式化为2010-03-18 10:50:00

例:

  • 10:50需要10:45
  • 1:12需要是一点
  • 3:28需要3:15
  • 等等

我假设floor()被涉及,但不知道如何去做。

谢谢

你的全部function是这样的…

 function roundToQuarterHour($timestring) { $minutes = date('i', strtotime($timestring)); return $minutes - ($minutes % 15); } 
 $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 :如果你想圆下来,floor()代替round() floor()

 $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; 

要使用下面的代码四舍五入,

 <?php $time = strtotime("01:08"); echo $time.'<br />'; $round = 15*60; $rounded = round($time / $round) * $round; echo date("H:i", $rounded); ?> 

01:08成为01:15

 $minutes = ($minutes - ($minutes % 15)); 

最近我喜欢解决一个TDD /unit testing方式的问题。 最近我不再编程太多的PHP,但这是我想出来的。 说实话,我实际上是看了这里的代码示例,并select了一个我认为是正确的。 接下来,我想通过使用上面提供的testing进行unit testing来validation这一点。

类TimeTest

 require_once 'PHPUnit/Framework.php'; require_once 'Time.php'; class TimeTest extends PHPUnit_Framework_TestCase { protected $time; protected function setUp() { $this->time = new Time(10, 50); } public function testConstructingTime() { $this->assertEquals("10:50", $this->time->getTime()); $this->assertEquals("10", $this->time->getHours()); $this->assertEquals("50", $this->time->getMinutes()); } public function testCreatingTimeFromString() { $myTime = Time::create("10:50"); $this->assertEquals("10", $myTime->getHours()); $this->assertEquals("50", $myTime->getMinutes()); } public function testComparingTimes() { $timeEquals = new Time(10, 50); $this->assertTrue($this->time->equals($timeEquals)); $timeNotEquals = new Time(10, 44); $this->assertFalse($this->time->equals($timeNotEquals)); } public function testRoundingTimes() { // Round test time. $roundedTime = $this->time->round(); $this->assertEquals("10", $roundedTime->getHours()); $this->assertEquals("45", $roundedTime->getMinutes()); // Test some more times. $timesToTest = array( array(new Time(1,00), new Time(1,12)), array(new Time(3,15), new Time(3,28)), array(new Time(1,00), new Time(1,12)), ); foreach($timesToTest as $timeToTest) { $this->assertTrue($timeToTest[0]->equals($timeToTest[0]->round())); } } } 

上课时间

 <?php class Time { private $hours; private $minutes; public static function create($timestr) { $hours = date('g', strtotime($timestr)); $minutes = date('i', strtotime($timestr)); return new Time($hours, $minutes); } public function __construct($hours, $minutes) { $this->hours = $hours; $this->minutes = $minutes; } public function equals(Time $time) { return $this->hours == $time->getHours() && $this->minutes == $time->getMinutes(); } public function round() { $roundedMinutes = $this->minutes - ($this->minutes % 15); return new Time($this->hours, $roundedMinutes); } public function getTime() { return $this->hours . ":" . $this->minutes; } public function getHours() { return $this->hours; } public function getMinutes() { return $this->minutes; } } 

运行testing

 alfred@alfred-laptop:~/htdocs/time$ phpunit TimeTest.php PHPUnit 3.3.17 by Sebastian Bergmann. .... Time: 0 seconds OK (4 tests, 12 assertions) 

对于我的系统,我想添加计划在服务器上每5分钟运行一次的作业,并且我希望在下一个第5分钟的时间段内运行相同的作业,然后分别为15,30,60,120,240分钟,1天和2天之后,这就是这个函数计算的

 function calculateJobTimes() { $now = time(); IF($now %300) { $lastTime = $now - ($now % 300); } ELSE { $lastTime = $now; } $next[] = $lastTime + 300; $next[] = $lastTime + 900; $next[] = $lastTime + 1800; $next[] = $lastTime + 3600; $next[] = $lastTime + 7200; $next[] = $lastTime + 14400; $next[] = $lastTime + 86400; $next[] = $lastTime + 172800; return $next; } echo "The time now is ".date("Ymd H:i:s")."<br /> Jobs will be scheduled to run at the following times:<br /><br /> <ul>"; foreach(calculateJobTimes() as $jTime) { echo "<li>".date("Ymd H:i:s", $jTime).'</li>'; } echo '</ul>'; 

我需要一种方法来整理当天的情况,并切断所有的事情:

 $explodedDate = explode("T", gmdate("c",strtotime("now"))); $expireNowDate = date_create($explodedDate[0]); 

strtotime给了我一个“现在”的时间戳,其中gmdate转换为ISO格式(如“2012-06-05T04:00:00 + 00:00”),然后我在“T”使用爆炸,给我“ 2012-06-05“在$ explodedDate的第零个索引中,然后将其传递到date_create中以获取date对象。

不知道是否所有这一切都是必要的,但似乎比通过减去秒,分钟,小时等工作less得多。

 // time = '16:58' // type = auto, up, down function round_time( $time, $round_to_minutes = 5, $type = 'auto' ) { $round = array( 'auto' => 'round', 'up' => 'ceil', 'down' => 'floor' ); $round = @$round[ $type ] ? $round[ $type ] : 'round'; $seconds = $round_to_minutes * 60; return date( 'H:i', $round( strtotime( $time ) / $seconds ) * $seconds ); } 

这是一个古老的问题,但最近实施自己我会分享我的解决scheme:

 public function roundToQuarterHour($datetime) { $datetime = ($datetime instanceof DateTime) ? $datetime : new DateTime($datetime); return $datetime->setTime($datetime->format('H'), ($i = $datetime->format('i')) - ($i % 15)); } public function someQuarterHourEvent() { print_r($this->roundToQuarterHour(new DateTime())); print_r($this->roundToQuarterHour('2016-10-19 10:50:00')); print_r($this->roundToQuarterHour('2016-10-19 13:12:00')); print_r($this->roundToQuarterHour('2016-10-19 15:28:00')); } 

我写了一个函数,把时间戳记为秒或分钟。

我可能不是最高性能的方式,但是我认为PHP并不关心几个简单的循环。

在你的情况下,你只需要像这样传递你的MySQLdate时间:

 <?php echo date('d/m/Y - H:i:s', roundTime(strtotime($MysqlDateTime), 'i', 15)); ?> 

返回:最接近的舍入值(看上去和下来!)

function:

 <?php function roundTime($time, $entity = 'i', $value = 15){ // prevent big loops if(strpos('is', $entity) === false){ return $time; } // up down counters $loopsUp = $loopsDown = 0; // loop up $loop = $time; while(date($entity, $loop) % $value != 0){ $loopsUp++; $loop++; } $return = $loop; // loop down $loop = $time; while(date($entity, $loop) % $value != 0){ $loopsDown++; $loop--; if($loopsDown > $loopsUp){ $loop = $return; break; } } $return = $loop; // round seconds down if($entity == 'i' && date('s', $return) != 0){ while(intval(date('s', $return)) != 0){ $return--; } } return $return; } ?> 

你简单地用's'来代替$ entity,如果你想四舍五入到秒,并用你想要上下的秒数或分钟数replace15。

这是我目前使用的一个函数:

 /** * Rounds a timestamp * * @param int $input current timestamp * @param int $round_to_minutes rounds to this minute * @param string $type auto, ceil, floor * @return int rounded timestamp */ static function roundToClosestMinute($input = 0, $round_to_minutes = 5, $type = 'auto') { $now = !$input ? time() : (int)$input; $seconds = $round_to_minutes * 60; $floored = $seconds * floor($now / $seconds); $ceiled = $seconds * ceil($now / $seconds); switch ($type) { default: $rounded = ($now - $floored < $ceiled - $now) ? $floored : $ceiled; break; case 'ceil': $rounded = $ceiled; break; case 'floor': $rounded = $floored; break; } return $rounded ? $rounded : $input; } 

希望它可以帮助别人:)

可能帮助别人。 对于任何语言。

 roundedMinutes = yourRoundFun(Minutes / interval) * interval. 

例如,间隔可能是5分钟,10分钟,15分钟,30分钟。 然后四舍五入的分钟可以重置为相应的date。

 yourDateObj.setMinutes(0) yourDateObj.setMinutes(roundedMinutes) 

简单的scheme:

 $oldDate = "2010-03-18 10:50:00"; $date = date("Ymd H:i:s", floor(strtotime($oldDate) / 15 / 60) * 15 * 60); 

如果你想整理,你可以更换floor