我将以dd-mm-yyyy格式从PHP表单中获取两个日期。
(比如说01/06/2013和30/06/2013)
现在我使用以下代码在上面给出的日期范围中显示日期结果,但它不适用于我,因为存储在DB中的日期是UNIX时间戳格式(transaction_date bigint(12)
)。那我该如何显示日期结果呢?任何人都可以帮我解决这个问题吗?
if($form_data['from_date']!='' && $form_data['to_date']!='') {
$from_time = explode("/", $form_data['from_date']);
$to_time = explode("/", $form_data['to_date']);
$start_date = mktime( 0,0,0,$from_time[1],$from_time[0],$from_time[2] ) ;
$end_date = mktime( 23,59,59,$to_time[1],$to_time[0],$to_time[2] ) ;
$sql =" SELECT COUNT(*) `total count`, SUM(transaction_status = 'success') `success`, ";
$sql .=" SUM(transaction_status = 'inprocess') `inprocess`, SUM(transaction_status = 'fail') `fail`, ";
$sql .=" SUM(transaction_status = 'cancelled') `cancelled` FROM user_transaction ";
$sql .=" WHERE transaction_date >= '".$start_date."' AND transaction_date <= '".$end_date."' GROUP BY transaction_date ";
$this->mDb->Query( $sql);
$queryResult = $this->mDb->FetchArray();
}
提前致谢。
答案 0 :(得分:2)
使用FROM_UNIXTIME(transaction_date)
将其作为日期类型。 Info
答案 1 :(得分:0)
在最后一行查询中进行更改,并使用以下代码替换:
$sql .=" WHERE DATE_FORMAT(transaction_date,'%d-%m-%Y') >= '".$start_date."' AND DATE_FORMAT(transaction_date,'%d-%m-%Y') <= '".$end_date."' GROUP BY FROM_UNIXTIME(transaction_date)";
此链接对您MySQL convert datetime to Unix timestamp更有帮助。