2013-04-04 121 views
-4

我想計算兩個php日期之間的差異。但我得到以下錯誤:計算兩個日期之間的差異php

Notice: Undefined variable: difference in C:\wamp\www\HR version 1.3\Applicant_Workdetails.php on line 68

我也想知道如果它的最佳實踐來計算差異這種方式。

if (isset($_GET['success']) && empty($_GET['sucess'])) { 
    echo 'Submitted Successfully' . ' '; 
    printf("%d years, %d months, %d days\n", $difference->y, $difference->m, $difference->d); //This is line 68 
} else { 
    if (empty($_POST) === false && empty($errors) === true) { 

     $startdate = $_POST['StartDate']; 
     $enddate = $_POST['EndDate']; 
     $datetime1 = new DateTime($startdate); 
     $datetime2 = new DateTime($enddate); 
     $difference = $datetime1->diff($datetime2); 

     //Submit Workdetails to the database 
     $personal_workdetails = array(
      'IndustryName' => $_POST['IndustryName'], 
      'Occupation' => $_POST['Occupation'], 
      'Position' => $_POST['Position'], 
      'Job_description' => $_POST['Job_description'], 
      'StartDate' => $startdate, 
      'EndDate' => $enddate, 
      'Personid' => $Personid, 
      'Jobid' => $jobid); 
     personal_workdetails($personal_workdetails); 
     //redirect 
     header('Location: Applicant_workdetails.php?success'); 
     exit(); 
    } else if (empty($errors) === false) { 
     //output errors if the errors array is not empty 
     echo output($errors); 
    } 
} 
+1

那麼,(在該行之前),你實際定義$區別? – 2013-04-04 11:07:56

+0

http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php?rq=1 – 2013-04-04 11:07:59

+0

[嘗試搜索](https://www.google .ru/search?q =差異+ +兩個+日期+ php +網站之間的差異%012stackoverflow.com) – sectus 2013-04-04 11:08:04

回答

1

我看不出其中創建$差別,你可以請張貼整個文件,這樣我就可以看到問題出在哪裏?

你做了$差異的printf(),但是這個變量只是聲明瞭(據我所知)之後的一些行。

1

在「C:\ wamp \ www \ HR版本1.3 \ Applicant_Workdetails.php」中的第68行,您正在使用您未定義的變量。可能增加或減少什麼。我們很難找到,因爲我們不知道您發佈了哪部分代碼。

但只是按照指示。 PHP已經明確了錯誤在哪裏。

1

您可以使用DateTime::diff

$datetime1 = new DateTime("$start_date"); 
    $datetime2 = new DateTime("$end_date"); 
    $interval = $datetime1->diff($datetime2); 
    echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 
+0

閱讀問題,不僅僅是標題。 – itachi 2013-04-04 11:13:59

相關問題