如何修复1970-01-01返回的日期?

时间:2016-08-05 06:08:56

标签: php mysql

我想在一列的两个日期之间搜索结果,我将日期转换为y-m-d格式,但我得到了响应1970-01-01,我该怎么办?

$dToDate = date("Y-m-d", strtotime($aData['to_date']))." 00:00:01";

    $dFromDate = date("Y-m-d", strtotime($aData['from_date']))." 23:59:59";


    $sAndWhere  = "1 = 1";
    $sAndWhere .= " AND d.created_at between '".$dToDate.' AND '.$dFromDate."'";
    $sAndWhere .= " AND u.mood =".$sMood;

    $sAndWhere .= " AND d.activated = 1 AND d.deleted=0";
    $sSql = "SELECT 
                *
            FROM 
                diaries d
            LEFT JOIN 
                users u 
            ON 
              d.id_user = u.id_user
            WHERE ".$sAndWhere;

1 个答案:

答案 0 :(得分:0)

$dToDate = $dFromDate = "";
if(strtotime($aData['to_date']) > 0)
{
    $dToDate = date("Y-m-d", strtotime($aData['to_date']))." 00:00:01";
}

if(strtotime($aData['from_date']) > 0)
{
    $dFromDate = date("Y-m-d", strtotime($aData['from_date']))." 23:59:59";
}

$sAndWhere  = "1 = 1";
if(($dToDate != "")  && ($dFromDate!=""))
{
    $sAndWhere .= " AND d.created_at between '".$dToDate."' AND '".$dFromDate."'";
}
else if($dToDate != "")
{
    $sAndWhere .= " AND d.created_at = '".$dToDate."'";
}
else if($dFromDate != "")
{
    $sAndWhere .= " AND d.created_at = '".$dFromDate."'";
}
$sAndWhere .= " AND u.mood =".$sMood;

$sAndWhere .= " AND d.activated = 1 AND d.deleted=0";
$sSql = "SELECT 
            *
        FROM 
            diaries d
        LEFT JOIN 
            users u 
        ON 
          d.id_user = u.id_user
        WHERE ".$sAndWhere;

使用此代码