2013-02-14 88 views
-2

我在PHP 5.3中使用下面的代码,但现在我必须在5.4.11上运行它,它不起作用。没有错误页面上,我得到了500错误。如果我评论最后3行,一切都OK。PHP 5.4.11中的DateTime问题

$sql = "*****"; 
$query = mysqli_query($dbc, $sql) or die('Error selecting date range.'); 
$row = mysqli_fetch_array($query); 
$firstDate = $row['min']; 
$lastDate = $row['max']; 
$arrDates = array(); 
$date1 = new DateTime($firstDate); 
$date2 = new DateTime($lastDate); 
$interval = $date1->diff($date2); 

EDIT1:对的var_dump $ firstDate和$ lastDate,不可能得到一个$日期1和$ date2的作为一切都停止的那一刻我取消的对象。

$firstDate - string(10) "2013-01-27" 
$lastDate - string(10) "2013-02-06" 

有什么建议吗?

+1

检查日志。 – Leri 2013-02-14 09:58:21

+1

线索?也许是$ firstDate和$ lastDate中返回值的var_dump()? – 2013-02-14 09:58:33

+0

你可以打印'$ firstDate'和'$ lastDate?'的内容吗? – Naryl 2013-02-14 09:58:58

回答

1

我在过去发现,它是使用更可靠的静态createFromFormat方法生成的DateTime对象,如果你知道你的输入字符串的格式事先:服务器

$date1 = DateTime::createFromFormat($format, $firstDate); 
$date2 = DateTime::createFromFormat($format, $lastDate); 
$interval = $date1->diff($date2); 
+0

完美,但为什么不是其他代码工作?有任何想法吗? – Boris 2013-02-14 10:15:35

+1

不,我第一次使用在Ubuntu上完美工作的代码来体验它,但是在Mac上使用相同的PHP版本。我仍然无法解释它:( – shanethehat 2013-02-14 10:17:31