duanliao6077 2014-11-23 04:06
浏览 37
已采纳

从mysql表中获取哪些列类型为“timestamp”的数据?

In mysql database I've a date value which data type is timestamp. It's showing this value is something like this : 1415607900, 1394439900, 1412929500 etc.

Now In Html form I can pickup a date. Date value is m/d/y format and this date is for searching data to DB. I can pickup Date From or Date To or Both from this html form field.

Date from is searching data from pickup date to current date.
Date To is searching all data until picktup Date.
If both date value exsit then it's searching data between this 2 dates.

So how do I search data against this timestamp ?

Please check my following mysql query which I'm running but can't get right result. I'm highly appreciate your suggestions/solutions. Thank You.

if(!empty($ad_datefrom) && empty($ad_dateto)){    
    $getSearch .= "AND cd.created_date2 BETWEEN '$ad_datefrom' AND '$date' ";   
}

if(!empty($ad_dateto) && empty($ad_datefrom)){    
    $getSearch .= "AND cd.created_date2 BETWEEN '0000-00-00' AND '$ad_dateto'"; 
}

if(!empty($ad_datefrom) && !empty($ad_dateto)){
    $getSearch .= "AND cd.created_date2 BETWEEN '$ad_datefrom' AND '$ad_dateto' ";  
}

----------------------------------------------------------------------------------
cd = contact_details Table,  
created_date2 = date column (data type : timestamp)  
$date = current date which format is m/d/y

Update :

/* this is for Date from*/
if(!empty($ad_datefrom) && empty($ad_dateto)){    
        $getSearch .= "AND cd.created_date BETWEEN UNIX_TIMESTAMP('$ad_datefrom') AND UNIX_TIMESTAMP('$date') ";    
}

/* this is for Date To*/
if(!empty($ad_dateto) && empty($ad_datefrom)){    
    $getSearch .= "AND cd.created_date BETWEEN UNIX_TIMESTAMP('0000-00-00') AND UNIX_TIMESTAMP('$ad_dateto') "; 
}

/* this is for Date from and Date To*/
if(!empty($ad_datefrom) && !empty($ad_dateto)){
    $getSearch .= "AND cd.created_date BETWEEN UNIX_TIMESTAMP('$ad_datefrom') AND UNIX_TIMESTAMP('$ad_dateto')";    
}
  • 写回答

1条回答 默认 最新

  • duanao6704 2014-11-23 04:14
    关注

    You can use FROM_UNIXTIME() to convert Unix time into date time.

    Here is an example SELECT * FROM tbl where FROM_UNIXTIME(dt) between '2014-10-01 00:00:00' and "2014-11-01 00:00:00'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容