2014-10-03 108 views
0

我想獲得兩個日期之間的小時數,但它似乎沒有給我正確的結果。瞭解PHP日期差異

global $wpdb; 
global $wp; 
$wp->query_vars['sp_date'] = "10/06/2014"; 
$wp->query_vars['sp_time_period'] = "2pm to 3pm"; 
// only process requests with "my-plugin=ajax-handler" 
    date_default_timezone_set('America/New_York'); 
    $date = false; 
    $time_span = false; 
    $dayoffs = false; 
    if(isset($wp->query_vars['sp_date'])) { 
     $date = $wp->query_vars['sp_date']; 
    } 
    if(isset($wp->query_vars['sp_time_period'])) { 
     $period = $wp->query_vars['sp_time_period']; 
    } 
    if(isset($wp->query_vars['get_dayoffs'])) { 
     $dayoffs = $wp->query_vars['get_dayoffs']; 
    } 
    $returnValue = 0; 
    if($date || $period) { 
     $hrs = 0; 
     if($date && !$returnValue) { 
      $_SESSION['sp_date'] = $date; 
      $returnValue = 1; 
     } 
     if($period && !$returnValue) { 
      $_SESSION['sp_time_period'] = $period; 
      $returnValue = 1; 
     } 
     if($_SESSION['sp_date']) { 
      $hrsTilNoonBool = 0; 
      $nToday = date("Y-m-d h:i:s"); 
      $deliveryDates = $_SESSION['sp_date']." 18:00:00"; 
      $deliveryDateNoon = $_SESSION['sp_date']." 12:00:00"; 

      $date1 = new DateTime($nToday); 
      $date2 = new DateTime($deliveryDates); 
      $date3 = new DateTime($deliveryDateNoon); 
      $diff = $date2->diff($date1); 
      $diffNoon = $date3->diff($date1); 
      $hrs = $diff->format('%H'); 
      $hrsTilNoon = $diffNoon->format('%h'); 
      if($hrsTilNoon) { 
       $hrsTilNoonBool = 1; 
      } 
     } 
     $settings = $wpdb->get_results("SELECT * FROM sp_settings WHERE id = 1"); 
     $content = '{"value":'.$returnValue.', "over_twentyfour":'.$hrs; 
     if(!$settings[0]->payment_id_before) 
      $settings[0]->payment_id_before = 53; 
     if(!$settings[0]->payment_id_after) 
      $settings[0]->payment_id_after = 54; 
     $content .= ',"payment_before":'.$settings[0]->payment_id_before.',"payment_after":'.$settings[0]->payment_id_after.',"before_afternoon":'. 
      $hrsTilNoonBool.'}'; 
    } 
    if($dayoffs) { 
     $dates = $wpdb->get_results("SELECT * FROM sp_date_blacklist"); 
     $datesArry = array(); 
     foreach($dates as $d) { 
      $datesArry[] = $d->date; 
     } 
     $content = json_encode($datesArry); 
    } 
    echo $content; 
    exit(); 

我第一次把一個目標日期$wp->query_vars['sp_date'] = "10/06/2014";然後我把今天的日期是2014年10月3日。

 $diff = $date2->diff($date1); 
     $diffNoon = $date3->diff($date1); 
     $hrs = $diff->format('%H'); 

日子說3是對的。但時間是14個小時?我很困惑。也許,也許我沒有讀一些正確

enter image description here

回答

1

嘗試追加同時每個時間戳,例如'00:00:00'。當你取得今天的日期時,也需要現在的時間。創建日期默認爲午夜。

1

你哪裏不曖昧日期:

$d1 = '01/02/2013'; 
$d2 = '01-02-2013'; 

$ts1 = strtotime($d1); // 1357106400 
$ts2 = strtotime($d2); // 1359698400 

echo date('r', $ts1); // Wed, 02 Jan 2013 00:00:00 -0600 
echo date('r', $ts2); // Fri, 01 Feb 2013 00:00:00 -0600 

/ V.S. strtotime()中的-分隔符是否有重大區別。這是歐洲的dd-mm-yyyy v.s.美國mm/dd/yyyy

您應該使用date_create_from_format()和EXCPLICITLY指定輸入日期字符串的格式。

+0

我不認爲這是有區別的,而不是歐洲人。即使我改變值來匹配格式,它仍然給我不正確的通知。 – numerical25 2014-10-06 11:54:48