1 回答
TA贡献1803条经验 获得超3个赞
在 TimeMeta 数组中,它包含子数组,子数组包含多个保留时间数组,这需要嵌套的 foreach 循环。所以 TimeMeta 数组可能如下
$time_meta = [
[
'id' => '9',
'start_date' => '2019-06-19',
'end_date' => '2019-06-19',
'start_time' => '08:00:00',
'end_time' => '09:00:00'
],
[
'id' => '10',
'start_date' => '2019-06-20',
'end_date' => '2019-06-20',
'start_time' => '08:00:00',
'end_time' => '09:00:00'
],
[
'id' => '11',
'start_date' => '2019-06-21',
'end_date' => '2019-06-20',
'start_time' => '08:00:00',
'end_time' => '09:00:00'
],
[
'id' => '12',
'start_date' => '2019-06-22',
'end_date' => '2019-06-20',
'start_time' => '08:00:00',
'end_time' => '09:00:00'
],
[
'id' => '13',
'start_date' => '2019-06-23',
'end_date' => '2019-06-23',
'start_time' => '08:00:00',
'end_time' => '09:00:00'
]
];
和 Refunds 数组可能如下。
$refunds = [
[
'before_hours' => '12',
'refund_percent' => '50'
],
[
'before_hours' => '24',
'refund_percent' => '70'
],
[
'before_hours' => '48',
'refund_percent' => '90'
]
];
在 TimeMeta 数组中,start_date 和 start_time 格式是正确的日期和时间格式,因此无需转换格式 'Ym-d'。
试试下面的代码,
foreach($time_meta as $data)
{
$start_datetime = $data['start_date']." ".$data['start_time'];
$start_datetime = \Carbon\Carbon::parse($start_datetime);
$diff = \Carbon::now()->diffInHours($start_datetime, false);
// NEED CODE HERE TO DETERMINE THE EXACT REFUND PERCENT
// ACCORDING TO TIME DIFFERENCE BETWEEN NOW AND THE BOOKED DATE
$refund_per = 0;
if($diff > 0)
{
foreach($refunds as $re)
{
$hr = $re['before_hours'];
if($diff < intval($hr))
{
break;
}
else if($diff == intval($hr))
{
$refund_per = $re['refund_percent'];
break;
}
$refund_per = $re['refund_percent'];
}
}
if($refund_per != 0)
{
echo "STARTING DATE :".$start_datetime." REFUND PERCENTAGE :".$refund_per ."<BR>";
}
else
{
echo "STARTING DATE :".$start_datetime." NO REFUND <BR>";
}
}
如果 'before_hours' 值在 Refunds 数组中按升序排序,则此示例代码可以正常工作。
- 1 回答
- 0 关注
- 180 浏览
添加回答
举报