• Status: Solved
• Priority: Medium
• Security: Public
• Views: 322

# if expired date

\$date is in format 2011-08-30
if \$date is ealier than today  (expired)
do something

if (\$date < time())
does not work
0
rgb192
5 Solutions

Commented:
\$today = date(Y-m-d);
if (\$date < \$today) {
//do something
}

0

Commented:

\$date = "2011-08-30";
\$today = date(Y-m-d);
if (\$date < \$today) {
//do something
}
0

Commented:
\$d1->format("Y-m-d");
\$d2 = date(Y-m-d);

\$Hourdiff = floor((\$d2-\$d1)/3600) ;
\$Minutediff =floor((\$d2-\$d1)/60);
\$Monthdiff =floor((\$d2-\$d1)/2628000);
\$daydiff =floor((\$d2-\$d1)/86400) ;
\$yeardiff =floor((\$d2-\$d1)/31536000) ;

if (\$daydiff>0)
{
// write year code here
}

0

Commented:
time() returns the number of seconds since the UNIX epoch. At the time of this writing, it would be "1320046318". Your \$date variable stores a string. You're wanting to compare a string, such as "2011-08-30", to a number. You cannot do math on stuff that isn't numbers. Easiest to do would be to use the strtotime() function to convert the string into a time, so you can do your comparison math.
0

Commented:
As an example,
``````if(strtotime(\$date)<time()){
echo "The given date is before the current time";
}
``````

let it be known, that you could give the current date, and have it still be seen as before the current time using this method. I'd personally not use the time() function in that manner you specified, and instead calculate it based on the number of days instead of seconds.
0

Commented:
Hello

``````\$exp_date = "2011-08-30";

\$todays_date = date("Y-m-d");

\$today = strtotime(\$todays_date);

\$expiration_date = strtotime(\$exp_date);

if (\$expiration_date > \$today)
{
\$valid = "yes";

}
else
{
\$valid = "no";

}
``````

Thank You.

Amar.
0

Commented:
This article teaches the ways to use PHP and MySQL to handle DATETIME issues:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

ISO-8601 DATETIME strings are described there. They are things like '2011-08-30'  Timestamps are also described there.  Timestamps are integer values giving the number of seconds since the beginning of the Unix Epoch.  The PHP function time() returns a timestamp which cannot be directly compared to an ISO-8601 string.  You must normalize the data somehow.  Either a timestamp or the ISO string must be used for each variable.

This comparison would work:

\$old = '2011-08-30';
\$new = date('c');
if (\$old < \$new) echo "\$old IS IN THE PAST";
0

Commented:
Please note, I made a mistake in my original code. I missed the ' ' in the date() function.

Use below:

\$date = "2011-08-30";
\$today = date('Y-m-d');
if (\$date < \$today) {
//do something
}

0

Author Commented:
Thanks all
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.