2012-11-02 107 views
1

我已經創建了一個函數來生成一週前,今天和一週後的生日成員列表。該功能是PHP日期比較找到時間戳1970年之前的生日?

$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-dd1960-06-12。此代碼在1周之前和今天工作正常,但返回後1周內,如果年份低於1970年,並且條件完全失敗後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); 
      } 


     } 



     ?> 
+0

試試上面的代碼 –

+0

使用爆炸和mktime將工作,但是當我們在年底增加像日期+7像30日,它將返回同一個月的第6日 –

+0

不返回下個月第6日, –

3

我認爲你的問題是從UNIX紀元因爲strtotime回報秒,這實際上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 
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); 
      } 
     } 
+0

我試過它返回相同的東西。 Vyacheslav Voronchuk說,我認爲我必須在db中做到這一點。 –

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函數本身的任何想法。

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>"; 

      ?>