简体   繁体   中英

comparing mysql dates using timestamps

I am using code like this to capture time related records.

Here is a sql fragment ...

$SQL_p .= " and UNIX_TIMESTAMP(".$data['task_filter'].") >= " . 
          strtotime(date('Y-m-d h:i:s',strtotime(str_replace('/','-',$data['datepicker'].' 00:00:00'))));
$SQL_p .= " and UNIX_TIMESTAMP(".$data['task_filter'].") <= " . 
          strtotime(date('Y-m-d h:i:s',strtotime(str_replace('/','-',$data['datepicker2'].' 23:59:59'))));

Dates are entered using the format dd/mm/YYYY and converted using str_replace('/','-',$data['datepicker2']) where $data['datepicker2'] is the var holding the date

Using php5 on the command line for testing, when I enter ....

echo date('Y-m-d h:i:s',strtotime(str_replace('/','-','25/11/2011 00:00:00')));

I get 2011-11-25 12:00:00 BUT what I expect is 2011-11-25 00:00:00

Also

echo date('Y-m-d h:i:s',strtotime(str_replace('/','-','25/11/2011 23:59:59')));

returns 2011-11-25 11:59:59 NOT the expected 2011-11-25 23:59:59 .

What am I doing wrong ?

Try:

echo date('Y-m-d H:i:s',strtotime(str_replace('/','-','25/11/2011 00:00:00')));

The "h" means 12 hour format, "H" means 24 hour format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM