我创建了一个功能来生成1周前,今天和之后一周生日的成员列表。功能是
$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);
}
}
从数据库返回的日期格式为yyyy-mm-dd
,即1960-06-12
。如果年份低于1970年和条件完全失败后1周,此代码可以在1周前和今天正常工作,但在返回后1周。
有人能为我提供正确的日期操作吗?
答案 0 :(得分:3)
我认为你的麻烦是因为strtotime
从UNIX纪元返回秒,实际上是1970年。我最好直接在数据库中比较日期:SELECT ADDDATE('2008-01-02', INTERVAL 1 WEEK);
或SELECT SUBDATE('2008-01-02', INTERVAL 1 WEEK);
你也可以这样做:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
if((integer) $interval->format('%R%a') > 7) // More than week
答案 1 :(得分:1)
试试这个:
<?php
$m= date("m"); // Month value
$de= date("d"); //today's date
$y= date("Y"); // Year value
for($i=0;$i<count($members);$i++){
$mem_bod_explode=explode("-",$members[$i]->dob);
$m_bday = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2],$y);
//$bday_date = date('d',$m_bday);
//$bday_month = date('m',$m_bday);
//$bday = $m_bday;
//echo $bday_date."</br>".$bday_month."</br>".$members[$i]->dob."</br>".date('Y-m-d',$bday);exit;
$week_before = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]-7,$y);
$week_after = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]+7,$y);
echo date('Y-m-d',$week_before);
echo"<br>";
echo date('Y-m-d',$week_after);
echo"<br>";
echo date('Y-m-d');
if(date('Y-m-d') == date('Y-m-d',$m_bday)){
$present_bday[] = array('mem_name'=> $members[$i]->name);
}
else if(date('Y-m-d',$m_bday)<date('Y-m-d') && date('Y-m-d',$m_bday)>=date('Y-m-d',$week_before)){
$past_bday[] = array('mem_name'=> $members[$i]->name);
}
else if(date('Y-m-d',$m_bday)>date('Y-m-d') && date('Y-m-d',$m_bday)<=date('Y-m-d',$week_after)){
$future_bday[] = array('mem_name'=> $members[$i]->name);
}
}
?>
答案 2 :(得分:0)
试试此代码。希望这对您有帮助。
$m= date("m"); // Month value
$de= date("d"); //today's date
$y= date("Y"); // Year value
for($i=0;$i<count($members);$i++){
$mem_bod_explode=explode("-",$members[$i]->dob);
$m_bday = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2],$mem_bod_explode[0]);
$bday_date = date('d',$m_bday);
$bday_month = date('m',$m_bday);
$bday = $m_bday;
//echo $bday_date."</br>".$bday_month."</br>".$members[$i]->dob."</br>".date('Y-m-d',$bday);exit;
$week_before = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]-7,$mem_bod_explode[0]);
$week_after = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]+7,$mem_bod_explode[0]);
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);
}
}
答案 3 :(得分:0)
我比较了数据库查询中的日期本身提出了类似这样的事情
$res = $this->db->query("SELECT * FROM members WHERE DAYOFYEAR(curdate()) - 7 <= dayofyear(dob) AND DAYOFYEAR(curdate()) +7 >= dayofyear(dob);")->result();
$weekbfr_mem = $this->db->query("SELECT * FROM members WHERE DAYOFYEAR(curdate()) - 7 <= dayofyear(dob) AND dayofyear(dob) < DAYOFYEAR(curdate());")->result();
$bday_mem = $this->db->query("SELECT * FROM members WHERE DAYOFYEAR(curdate()) = dayofyear(dob);")->result();
$weekafter_mem = $this->db->query("SELECT * FROM members WHERE DAYOFYEAR(curdate()) < dayofyear(dob) AND dayofyear(dob) <= DAYOFYEAR(curdate()+7);")->result();
这个主要的disad我必须对服务器进行3次查询,对于project.so任何关于在php函数本身做的事情。
答案 4 :(得分:0)
Try the below code for testing:
I have putted date 1983-10-30 and its showing in Past.
<?php
$m= date("m"); // Month value
$de= date("d"); //today's date
$y= date("Y"); // Year value
for($i=0;$i<1;$i++){
$mem_bod_explode=explode("-","1983-10-30");
$m_bday = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2],$y);
//$bday_date = date('d',$m_bday);
//$bday_month = date('m',$m_bday);
//$bday = $m_bday;
//echo $bday_date."</br>".$bday_month."</br>".$members[$i]->dob."</br>".date('Y-m-d',$bday);exit;
$week_before = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]-7,$y);
$week_after = mktime(0,0,0,$mem_bod_explode[1],$mem_bod_explode[2]+7,$y);
echo date('Y-m-d',$week_before);
echo"<br>";
echo date('Y-m-d',$week_after);
echo"<br>";
echo date('Y-m-d');
if(date('Y-m-d') == date('Y-m-d',$m_bday)){
$present_bday[] = array('mem_name'=> "fgfg");
}
else if(date('Y-m-d',$m_bday)<date('Y-m-d') && date('Y-m-d',$m_bday)>=date('Y-m-d',$week_before)){
$past_bday[] = array('mem_name' => "gfgfg");
}
else if(date('Y-m-d',$m_bday)>date('Y-m-d') && date('Y-m-d',$m_bday)<=date('Y-m-d',$week_after)){
$future_bday[] = array($members[$i]->name);
}
}
echo"<br>";
echo"Past Birthday:<br>";
print_r($past_bday);
echo"<br>";
echo"Present Birthday:<br>";
print_r($present_bday);
echo"<br>";
echo"Future Birthday:<br>";
print_r($future_bday);
echo"<br>";
?>