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);
            }
        }

从 db 返回的日期格式是yyyy-mm-ddie 1960-06-12。此代码在之前和今天的 1 周内正常工作,但如果年份低于 1970 年和条件完全失败后的 1 周,则在返回后 1 周工作。

任何人都可以为我提供正确的日期操作以获得正确的结果吗?

4

5 回答 5

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
于 2012-11-02T06:32:02.327 回答
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);
            }


        }



        ?>
于 2012-11-02T07:40:24.133 回答
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);
            }
        }
于 2012-11-02T06:53:25.623 回答
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();

主要的缺点是我必须进行 3 次查询才能为 project.so 提供缓慢的服务。所以关于在 php 函数本身中做的任何想法。

于 2012-11-02T10:08:52.007 回答
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>";

            ?>
于 2012-11-02T10:37:18.887 回答