PHP查询中的PHP日期时间格式

时间:2016-03-15 06:45:54

标签: php sql

我正在执行我的分析代码,该代码将显示从现在开始的7个销售报告。

这是我的完整代码:

<?php
include 'report/go.php';

$to_time =time();
$to_time = date("Y-m-d H:i:s",$to_time);
$from_time = time()-6*60*60;
$from_time = date("Y-m-d H:i:s",$from_time);


    $sql = "SELECT 
            *
            FROM Tickets
            WHERE LastUpdateTime BETWEEN $from_time and $to_time";

    $stmt = sqlsrv_query( $conn, $sql );
        if( $stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
                $Date = $row['LastUpdateTime']->format('d/m/Y');
                $Time = $row['LastUpdateTime']->format('H:i:s');
                $Hour = $row['LastUpdateTime']->format('H');
            echo $row['Id']." - ".$Date." - ".$Time." - ".$Hour." Total amount: ".floor($row['TotalAmount'])."<br />";
        }

sqlsrv_free_stmt( $stmt);
?>

我的代码不起作用。我相信我在日期转换或某种事情上遇到了一些问题。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:5)

datetime应该包含在'中,因为它们是string

"SELECT 
*
FROM Tickets
WHERE LastUpdateTime BETWEEN '$from_time' and '$to_time'"

计算$from_time只能做 -

$from_time = date("Y-m-d H:i:s", strtotime('- 6 HOURS'));

而不是

$from_time = time()-6*60*60;
$from_time = date("Y-m-d H:i:s",$from_time);