我試圖在兩個日期之間獲得天數差異。第一天是當天。另一天是他們的生日。我想計算今天和他們下一個生日之間的差異。目前,如果用戶今天輸入他們的bday,它會正確輸出,但是其他任何東西都會被混淆。我想按天計算一下。php日期差異
例如,如果我輸入我的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/>";
}
}
這需要進行基本的調試。什麼時候出問題了?測試輸出是什麼'month',''day'和'year'顯示? – 2012-03-01 08:34:11
它們顯示用戶輸入的日期,例如,如果我輸入09/15/1988,那麼$ month將是09,$ day將是15,$ year將是1988. – stytown 2012-03-01 08:36:21
好吧,然後進一步調試。每次操作後都要測試每個值的輸出。它在哪一點脫軌? – 2012-03-01 08:49:06