Forum Moderators: coopster

Message Too Old, No Replies

Date Comparison

Checking whether one date comes earlier....

         

Francis

8:48 am on Jan 6, 2006 (gmt 0)

10+ Year Member



Here is my code:

--------------
$expiry = $row["end_date"];

$today = strtotime("now");
$date_today = date('m/d/y',$today);
if ($expiry > $date_today) {
$new_status == "Expire";
} else {
$new_status == $row["status"];
}
--------------

What I want to happen is to compare the $expiry with the $date_today. If the $expiry is after the date_today (meaning it has passed already), then the $new_status would have "Expire" as its value. Otherwise, it will just get the value from the database and display it as it is.

The $row["end_date"] that comes from the database is of varchar field.

Thanks.

proper_bo

8:58 am on Jan 6, 2006 (gmt 0)

10+ Year Member



What you want to do is turn both into unix time and then compare them.

So use strtotime on todays date and on the date you pull from the database and then see which is greater.


$expiry = strtotime($row["end_date"]);
$today = strtotime("now");
//$date_today = date('m/d/y',$today); - no longer needed

if ($expiry > $today) {
$new_status == "Expire";
} else {
$new_status == $row["status"];
}

Think thats right, but surely it should be if expiry is less than todays date? If it is greater then it is in the future?!

Francis

9:12 am on Jan 6, 2006 (gmt 0)

10+ Year Member



Thanks for the help.

However, after revising the code to the ones you suggested, nothing is now being displayed when I echo the $new_status.

Herewith is one sample value set:

$expiry $today
1159848000 --- 1136538649

Thanks for the help again and sorry for the bother.

proper_bo

9:21 am on Jan 6, 2006 (gmt 0)

10+ Year Member



the date you get from the db is in m/d/y format yes?
you want to know if the expiry date is before today in which case it has expired yes?

from the value set you have there you can see that is has correctly given you two unix time stamps so now you can just compare them.

Francis

9:26 am on Jan 6, 2006 (gmt 0)

10+ Year Member



i think this is where the problem lies because the field for the date from the database is in varchar. When the value inside the field is viewed, it is displayed as, for example, 1/09/06

proper_bo

9:31 am on Jan 6, 2006 (gmt 0)

10+ Year Member



as long as it has given you two unix time stamps using strtotime then you can compare them

if( $a > $b ){

//do something

}else{

//do something else

}

that code is correct and should be working fine.