【发布时间】:2022-01-25 11:00:40
【问题描述】:
您好,我正在尝试在 php 中创建一个函数来检查当前日期并返回商店是否打开或关闭。这是我到目前为止所拥有的,它有效,但它很长,所以如果有人知道最短路径,请帮忙!
$today = date('w'); // today
$starting = "THUR"; //opening day
$closing = "MON"; //closing day
$openday = date("w", strtotime($starting)); //coverting to number
$closeday = date("w", strtotime($closing)); // coverting to number
//checking if closing day is greater than opening day
if($openday < $closeday ){
//checking if today lies between starting and closing day
if($today > $openday && $today <= $closeday ){
$stat = 'Opened'; //if today lies between starting day and ending day then Opened
}
else{
$stat = 'Closed'; //else Closed
}
}
// if closing day is less than opening day
else{
//checking if today lies between opening day and friday
if($today > $openday && $today <= 6){
$stat = 'Opened';
}
//checking if today lies between sunday and closeday
else if( $today > 0 && $today <= $closeday ){
$stat = 'Opened';
}
else{
$stat = 'Closed'; // else Closed
}
}
如果日期介于星期四和星期一之间,则返回 Opened else Closed。如果您知道捷径,请帮助我
【问题讨论】:
标签: php