简体   繁体   中英

compare if a date has passed

So I have some events(party) and each one has a date, when the party will be, how can I check this against the current date? I need to find all events that have passed? Any ideas? I don't even know from where to start, I store the date in mysql like this: dd/mm/yy it's plain text because I user WP metabox.

Any help will be appreciated.

You should store your dates using the DATETIME format and use

SELECT date FROM dates WHERE DATEDIFF(date,NOW()) <= 0;

If you insist on keeping your very bad database scheme, you could instead do the conversion on the fly

SELECT STR_TO_DATE(date,'%d/%m/%Y') AS adate WHERE DATEDIFF(adate,NOW()) <= 0

Might be a good candidate for strtotime, which can convert your date to a timestamp which is easily compared against the current timestamp generated with the time() function.

http://php.net/manual/en/function.strtotime.php

// your db date format is dd/mm/yy (European). the American is mm/dd/yy 
$db_date_event = str_replace('/', '-', $db['date_event']); 
// when - or . is used in strtotime European d-m-y format is assumed    
$db_date_event = strtotime($db_date_event);

if (time() > $db_date_event)
{
    echo 'this event is in the past';
}

// To compare at day level: (each day is represented in the 0:00 hrs (day's begin)) 

if (mktime(0, 0, 0, date('n'), date('j'), date('Y')) > $db_date_event)
{
    echo 'this event is in the past';
}

Convert the date into a Unix timestamp:

list($d,$m,$y)=explode("/",$datestring);
$date=mktime(0,0,0,$m,$d,$y);

then compare with today:

$today=mktime(0,0,0,date("m"),date("d"),date("Y"));

using < , == or > :

if($date<$today)
{
     echo "Date is in the past";
}

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