$personas = [
'Joe' => [
'date' => ['start' => '1927-10-01','end' => '2009-11-24'],
'statement' => 'Opa'
],
'Pam' => [
'date' => ['start' => '1975-06-21','end' => ''], // empty means infinite
'statement' => 'Mother'
],
'Sara' => [
'date' => ['start' => '2020-01-01','end' => '2020-11-12'],
'statement' => 'cam on baby'
],
'Karl' => [
'date' => ['start' => '2019-11-11','end' => '2019-11-12'],
'statement' => 'fresh'
],
],只带走当期的人
(实际日期为2019年-11-11)
$actualDate = date("Y-m-d");
foreach($personas as $date => $person) {
if ($person['date']['start'] >= $startDate && $person['date']['end'] <= $endDate){
echo $person.': '.$person['statement']'<br>';
}
}预期:
帕姆:妈妈
卡尔:新鲜
发布于 2019-11-11 08:55:01
有几件事--您的测试是使用未定义的字段,我假设您想要根据实际日期字段($actualDate)计算它。我还添加了一个子句,如果记录中的结束日期是当前日期的empty()或>=,则结束日期与结束日期匹配(我还稍微更改了输出)。
另一件事是,您的数据对于Karl的输出是错误的,我假设您的意思是数据有一个'2019-11-12'的结束日期……
'Karl' => [
'date' => ['start' => '2019-11-11','end' => '2019-11-12'],
'statement' => 'fresh'
],而密码..。
$actualDate = date("Y-m-d");
foreach($personas as $name => $person) {
if ($person['date']['start'] <= $actualDate &&
(empty($person['date']['end']) || $person['date']['end'] >= $actualDate )){
echo $name.': '.$person['statement'].PHP_EOL;
}
}https://stackoverflow.com/questions/58798061
复制相似问题