我如何比较PHP中的日期与不同的格式Y / m / d和Y / m / d H:M存储在数据库中?

我如何比较PHP中的日期与不同的格式Y / m / d和Y / m / d H:M存储在数据库中?

问题描述:

I have a query like that:

$monitor = $db->query("SELECT * FROM event,  step WHERE step = idstep ");
$countquery = $monitor->rowCount();
if ($countquery == 0){
    echo "no records";
}else{
    echo "show records";

and i would like to be:

$today = ("Y/m/d");
$monitor = $db->query("SELECT * FROM event,  step WHERE step = idstep AND datastep = $today");
$countquery = $monitor->rowCount();
if ($countquery == 0){
    echo "no records";
}else{
    echo "show records";

In my database, datastep is stored like: Y/m/d H:M and when i compare the 2 dates i have no results. i would like to not consider hours and minutes. How can i compare the dates, without changing the field datastep in the database? I would like to convert datastep in Y/m/d format.

Use DateTime object.

$today = new DateTime("2014/07/31");

and the you can format it whatever you like:

$today->format("Y/m/d H:M");

But I think you could also use DATE() in mysql:

SELECT * FROM event, step WHERE step = idstep AND DATE(datastep) = DATE('$today');

DATE() function in mysql will return only day, in this way DATE('2014/07/31 12:00') will be equal to DATE('2014/07/31').