2015-09-05 37 views
1

我有一个我正在设计的web应用程序。它将允许管理员为约会动态设置“块大小”,以及“开放时间”。计算两次之间n分钟的块数

现在我需要一种方法来动态计算“开放时间”之间“块”的数量。

即block_size = 30(分钟)。有多少9:00和17:00

OR

的block_size = 60(分钟)在30钟座。有多少8:00至18:00在60钟座

OR

的时间和块大小任意组合

+0

你的意思是,地板((结束时间 - 开始时间)/ BLOCK_SIZE)? – uri2x

回答

0

尝试......

<?php 
$seconds = $blocksize * 60; // get the seconds of the blocksize 
$start = strtotime("$open"); // get epoch time 
$end = strtotime("$close"); // get epoch time 
$math = $end - $start; // in epoch time 
$blocks = $math/$seconds; 

echo 'result: '.$blocks; 
?> 

希望这会有帮助

+0

$ blocksize是由他给出的30或60那样 –

+0

如果$ blocksize是10,那么会发生什么情况。 $ blocksize可能有任何值 –

+0

看起来不错upvoted –

0

试试这个

<?php 
$start = strtotime("start time"); 
$end = strtotime("end time"); 
$elapsed = $end - $start; 
$min = date("i", $elapsed); 
$blocks = floor($min/block_size); 
echo $blocks; 
?> 
1

你可以用DatePeriod很容易地做到这一点:

$start = new DateTime('9:00'); 
$end = new DateTime('17:00'); 
$blockDuration = new DateInterval('PT30M') 
$blocks = new DatePeriod($start, $blockDuration, $end); 
echo count(iterator_to_array($blocks)); // 16 
相关问题