1

So I have a date stored in a table as a string (eg. 2-14-2014) I'm pretty sure that puts it in the format of m-j-Y, j because there are no leading zeros. How would I go about comparing this with todays date? I want to be able to have something that looks like:

if(event_date>today){
//send data to app
}else{
//delete item from table
}

Thank you in advance,

Tyler

TylerM
  • 153
  • 17

3 Answers3

1

You have to convert them both using strtotime , so suggested code would be :

$exp_date = "2014-01-16"; // or value from db
$todays_date = date("m-d-Y");

$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);

if ($expiration_date > $today) {
     // Do whatever you want
} 
naguib
  • 85
  • 8
1

So first you want to convert the date into a UNIX Timestamp by using strtotime

$event_date = strtotime($date);

It sounds like you just want to check if the $event_date is in the future so you could do

if($event_date > time() {
    //Event is in the future
} else {
    //Event isn't in the future
}
Pattle
  • 5,983
  • 8
  • 33
  • 56
  • Thank you that helped along with Brian Anderson. – TylerM Feb 24 '14 at 23:47
  • I know this is off topic but how would I go about deleting an item from my table in //Event isn't in the future ? I've tried $result1 = mysql_query("DELETE * FROM events WHERE pid = $pid_check"); but that is not working. I've also tried without the where, but still nothing – TylerM Feb 24 '14 at 23:56
1

You can use the strtotime function, but you'll need to format your date first:

$now = strtotime("now");
$dateArray = date_parse_from_format("n-j-Y", "2-12-2014");
$test = strtotime($dateArray['year'].'-'.$dateArray['month'].'-'.$dateArray['day']);
return $now > $test;
Brian Anderson
  • 621
  • 7
  • 22