php中的日期差异

时间:2012-03-01 08:29:57

标签: php datetime

我试图在几天之内得出两个日期之间的差异。第一个日期是当天。另一个日期是他们的日期。我想计算今天和下一个bday之间的差异。目前,如果用户输入他们的bday作为今天,它输出正确,但其他任何东西只是搞砸了。我想按天算一下。

例如,如果我输入我的bday作为01/11/1988,我的程序将输出0 Years, 1 Months, 21 Days。如果我可以正确输出年,月和日,那么我可以计算总天数,但截至目前它只是不能正常工作。谢谢参观。这是我的代码:

if (isset($_POST["birthday"])){
    $birthday=$_POST["birthday"];
    if (!empty($birthday)){
        $birthdayArray = explode("/", $birthday);
        if (count($birthdayArray) != 3  || strlen($birthday) != 10){
            echo "<b>Please follow the format (ex. 09/15/1988).</b><br/>";
            }else{
                $month = $birthdayArray[0];
                $day = $birthdayArray[1];
                $year = $birthdayArray[2];
                echo "Your bday is: ".$month."/".$day."/".$year;
                echo "<br/>";
                if (strcmp($currentMonth,$month) == 0 && strcmp($currentDay,$day) == 0){
                    echo "Your birthday is today.<br/>";
                }else{
                    $date1 = new DateTime("$currentYear-$currentMonth-$currentDay");
                    if (strcmp($currentMonth, $month) <= 0 && strcmp($currentDay, $day) < 0 ){
                        $currentYear = $currentYear + 1;
                        $date2 = new DateTime("$currentYear-$month-$day");
                    }else{
                        $date2 = new DateTime("$currentYear-$month-$day");
                    }
                    $interval = $date1->diff($date2);
                    echo $interval->y." Years, ".$interval->m." Months, ".$interval->d." Days.";

                }
            }
    }else{
        echo "<b>Please enter your birthday.</b><br/>";
    }
}

2 个答案:

答案 0 :(得分:1)

 if (strcmp($currentMonth, $month) <= 0 && strcmp($currentDay, $day) < 0 ){
            $currentYear = $currentYear + 1;

这种情况实际上在做什么?

如果当前月份少于生日月份,则表示当前年份增加。但你应该以相反的方式做。

可能是你试图实现这个:

  //assume BIRTHDAY : 03/24/1990    
 if(...)
    echo "today birthday ";
  else  
  {
   if(currentMonth > $month) // may be today : 04/21/2012
   {
     $currentYear++;
   }
   else if(currentMonth == $month) // may be today : 03/29/2012
   {
    if($currentDay > $day) // may be today : 03/29/2012
     {  $currentYear++; }
   }
  } 
  $date2 = new DateTime("$currentYear-$month-$day");

答案 1 :(得分:0)

请看一下这段代码,它可以随意发出。

    if (isset($_POST["birthday"])){
    $birthday=$_POST["birthday"];
    if (!empty($birthday)){
        $birthdayArray = explode("/", $birthday);
        if (count($birthdayArray) != 3  || strlen($birthday) != 10){
            echo "<b>Please follow the format (ex. 09/15/1988).</b><br/>";
            }else{
                $month = $birthdayArray[0];
                $day = $birthdayArray[1];
                $year = $birthdayArray[2];
                $currentMonth = date('m');
                $currentDay = date('d');
                $currentYear = date('Y');
                echo "Your bday is: ".$month."/".$day."/".$year;
                echo "<br/>";
                if (strcmp($currentMonth,$month) == 0 && strcmp($currentDay,$day) == 0){
                    echo "Your birthday is today.<br/>";
                }else{
                    $date1 = new DateTime("$currentYear-$currentMonth-$currentDay");
                    if (strcmp($currentMonth, $month) <= 0 && strcmp($currentDay, $day) < 0 ){
                        $currentYear = $currentYear + 1;
                        $date2 = new DateTime("$currentYear-$month-$day");
                    }else{
                        $date2 = new DateTime("$currentYear-$month-$day");
                    }
                    $interval = $date1->diff($date2);
                    echo $interval->y." Years, ".$interval->m." Months, ".$interval->d." Days.";

                }
            }
    }else{
        echo "<b>Please enter your birthday.</b><br/>";
    }
}