Himanshu Upadhyay Himanshu Upadhyay - 10 months ago 72
MySQL Question

Mysql compare date time

I have varchar field in database and format of that field is like '10 Feb 2016 08:26 PM'.

Now I want to fetch upcoming data, So how can i get that ?

$ctime=strtotime(date("Y-m-d H:i:s"));

$books = $objData->getAll("select *
from bookings
where custID='".$custID."'
and fromTime>'".$ctime."'");

But I am not getting correct result, please help.

Answer Source

First of all, as mentioned in comments you should use proper types for dates. To answer your question, it is still possible to achieve, using STR_TO_DATE mysql function.

$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());

Link: Convert VARCHAR timestamp to TIMESTAMP?