2014-06-08 47 views
-1

您好我想在Laravel 4中使用DateTime,並且我不斷調用成員函數add()嘗試和使用添加功能,不能找出原因。在Laravel的日期時間 - 調用一個非對象的成員函數add()

這是我的代碼

if(isset($_POST['from']) && isset($_POST['direction']) && isset($_POST['product']) && isset($_POST['operator'])){ 

     $businessid = mysql_real_escape_string($_POST['operator']); 
    $direction = mysql_real_escape_string($_POST['direction']); 

    $dateStart = DateTime::createFromFormat('D M d Y', $_POST['from']); 
    $dateEnd = DateTime::createFromFormat('D M d Y', $_POST['from']); 

     if($direction == 'first') 
     { 
      $dateEnd->add(new DateInterval('P14D')); 
     } 
     else if($direction == 'right') 
     { 
      $dateStart->add(new DateInterval('P1D')); 
      $dateEnd->add(new DateInterval('P7D')); 
     } 
     else if($direction == 'left') 
     { 
      $dateStart->sub(new DateInterval('P7D')); 
      if($dateStart < $todaysDate) { $dateStart = $todaysDate; } 
      $dateEnd->sub(new DateInterval('P1D')); 
     } 
     else if($direction == 'date') 
     { 
      $dateStart->sub(new DateInterval('P7D')); 
      while($dateStart < $todaysDate) { $dateStart->add(new DateInterval('P1D')); } 
      $dateEnd->add(new DateInterval('P10D')); 
     } 

任何想法將是一個很大的幫助的感謝,

回答

1

而不是使用它的靜態你可以簡單的實例化一個新的DateTime對象。考慮這個例子:

$from = '2014-06-06'; // sample data 
$dateEnd = new DateTime(); 
$dateEnd->createFromFormat('D M d Y', $from); 
$dateEnd->add(new DateInterval('P10D')); 

echo $dateEnd->format('D M d Y'); 
相關問題