Solved

# if expired date

Posted on 2011-10-30
304 Views
\$date is in format 2011-08-30
if \$date is ealier than today  (expired)
do something

if (\$date < time())
does not work
0
Question by:rgb192

LVL 17

Accepted Solution

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

0

LVL 17

Expert Comment

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

LVL 9

Assisted Solution

\$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

LVL 7

Assisted Solution

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

LVL 7

Expert Comment

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

LVL 11

Assisted Solution

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

LVL 107

Assisted Solution

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

LVL 17

Expert Comment

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 Closing Comment

Thanks all
0

## Featured Post

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.