我有一个数据库表,将餐厅的营业时间存储为time格式的时间范围。例如,如果餐厅的营业时间是"上午9点到下午5点",将有2列"hours_open"answers"hours_close",我在"hours_open"中存储9:00,在"hours_close"中存储17:00。我需要做的是显示时间范围,例如:9:30am 10:00am 10:30am 11:00am 11:30am直到5:00pm
谁能给我指个正确的方向?$start = strtotime('6:00am');
$end = strtotime('09:00am');
$range = array();
while ($start !== $end)
{
$start = strtotime('+30 minutes',$start);
$range[] = date('h:ia', $start);
}
print_r($range);
谁能给我指个正确的方向?
当然,这里是你需要的一切:
- http://php.net/manual/en/function.mktime.php
- http://php.net/manual/en/control-structures.while.php
- 30 m = 1800 s
- http://php.net/manual/en/language.operators.arithmetic.php
- http://php.net/manual/en/function.date.php
在我目前的项目中,我必须循环处理一天的24小时…并希望它们以特定的格式显示。
$interval = new DateInterval('PT1H'); // creating the interval for 1hour
$date = new DateTime();
$end = clone($date);
$hoursRange = new DatePeriod($date, $interval ,$end->modify("+24 hours"));
然后我所要做的就是使用foreach遍历hoursRange值。
foreach ($hoursRange as $key => $value)
{
echo $value->format('ga');
}