我们说我每个星期一和每个星期三都有办公时间。
我想显示"下一个办公时间:周三"这将自动显示与今天相关的下一个即将到来的办公日。
所以我有以下内容:
if($officeday >= $today) {
echo 'NEXT OFFICE HOURS: ' . $dowMap[$officeday];
}
我尝试用以下方法完成此任务:
def newMessage = new XmlParser().parseText(newMsg);
def newItems = newMessage.depthFirst().findAll{it.name() == 'Items'};
def existingMessage = new XmlParser().parse(src); //src is a file location
def existingSettlementItem = existingMessage.depthFirst().findAll{it.name() == 'Items'};
但是,比较大的比较并不一致,因为例如星期一不高于星期五。我如何修改它以获得下一个实际的下一天?
谢谢!
答案 0 :(得分:1)
更新
$today = date('w'); // returns 0,1,2,3....
$dowMap = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$officeday = 3 ;// Monday = 1, Wednesday = 3...
$firstday = 1;
if($officeday >= $today and $today <= 5) {
echo 'NEXT OFFICE HOURS: ' . $dowMap[$officeday+1];
}
else{
echo 'NEXT OFFICE HOURS: ' . $dowMap[$firstday];
}
结果是当officeday范围显示办公室日+ 1或($ dowMap [indexNext]); 否则将在变量$ firstday上显示索引第一天。
答案 1 :(得分:0)
这是一个有趣的方式:使用InfiniteIterator循环一周的日子。一旦达到可接受的日期,您就可以停止并返回该值:
ssl connection error: unknown error number
输出:
$availableOfficeHours = new class() { public function __construct() { //For added fun, use a SplEnum! $this->daysOfWeek = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', ]; $this->daysOfWeekIterator = new InfiniteIterator(new ArrayIterator($this->daysOfWeek)); $this->officeHours = [ 'Monday', 'Wednesday' ]; } public function getNextOfficeDay($today) { if(!in_array($today, $this->daysOfWeek)){ throw new InvalidArgumentException('$today must be a spelled out name of the week. Example: "Monday".'); } //Set the iterator to today... while($today !== $this->daysOfWeekIterator->current()){ $this->daysOfWeekIterator->next(); } do { $this->daysOfWeekIterator->next(); $day = $this->daysOfWeekIterator->current(); } while(!in_array($day, $this->officeHours)); return $day; } }; echo $availableOfficeHours->getNextOfficeDay('Monday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Tuesday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Wednesday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Thursday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Friday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Saturday') . PHP_EOL; echo $availableOfficeHours->getNextOfficeDay('Sunday') . PHP_EOL; //Get based on today's date: $today = date('l'); echo $availableOfficeHours->getNextOfficeDay($today) . PHP_EOL;
这是有效的,因为InfiniteIterator会自动循环而不必手动重置它,因此设置一周中的某一天将允许它旋转回来并在每周的第一天进行。这可能不是最实用的实用方式。我只是想玩一个InfiniteIterator。