我创建了函数来生成前一周、今天和一周后生日的成员列表。功能是
$m= date("m"); // Month value
$de= date("d"); //today's date
$y= date("Y"); // Year value
for($i=0;$i<count($members);$i++){
$m_bday = strtotime($members[$i]->dob);
$bday_date = date('d',$m_bday);
$bday_month = date('m',$m_bday);
$bday = strtotime(date('Y-m-d',mktime(0,0,0,$bday_month,$bday_date,$y)));
//echo $bday_date."</br>".$bday_month."</br>".$members[$i]->dob."</br>".date('Y-m-d',$bday);exit;
$week_before = strtotime(date('Y-m-d', mktime(0,0,0,$m,($de-7),$y)));
$week_after = strtotime(date('Y-m-d', mktime(0,0,0,$m,($de+7),$y)));
if(date('d-m') == date('d-m',$bday)){
$this->present_bday[] = array('mem_name'=> $members[$i]->name);
}
else if(date('d-m',$week_before) <= date('d-m',$bday) && date('d-m',$bday) < date('d-m')){
$this->past_bday[] = array('mem_name' => $members[$i]->name);
}
else if(date('d-m')< date('d-m',$bday) && date('d-m',$bday) <= date('d-m',$week_after)){
$this->future_bday[] = array('mem_name' => $members[$i]->name);
}
}
从 db 返回的日期格式是yyyy-mm-dd
ie 1960-06-12
。此代码在之前和今天的 1 周内正常工作,但如果年份低于 1970 年和条件完全失败后的 1 周,则在返回后 1 周工作。
任何人都可以为我提供正确的日期操作以获得正确的结果吗?