0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Uffo
  • 9,628
  • 24
  • 90
  • 154
  • possible duplicate of [Shortest way to compare 2 dates in dd/mm/yyyy format](http://stackoverflow.com/questions/2729680/shortest-way-to-compare-2-dates-in-dd-mm-yyyy-format) – Fraser Feb 22 '12 at 20:30
  • duplicate http://stackoverflow.com/questions/2355075/php-how-to-compare-date-and-date – Fraser Feb 22 '12 at 20:34
  • again - http://stackoverflow.com/questions/2113940/php-compare-date – Fraser Feb 22 '12 at 20:35

4 Answers4

2

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
kba
  • 19,333
  • 5
  • 62
  • 89
1

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

JustinW
  • 271
  • 1
  • 6
1
// 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';
}
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
0

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";
}
Gareth
  • 5,693
  • 3
  • 28
  • 37