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

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
Asked:
rgb192
5 Solutions
 
psimationCommented:
$today = date(Y-m-d);
if ($date < $today) {
//do something
}

0
 
psimationCommented:
Assuming your $date variable is set already, else do:

$date = "2011-08-30";
$today = date(Y-m-d);
if ($date < $today) {
//do something
}
0
 
keyuCommented:
$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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HellmarkCommented:
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
 
HellmarkCommented:
As an example,
if(strtotime($date)<time()){
     echo "The given date is before the current time";
}

Open in new window


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
 
Amar BardoliwalaCommented:
Hello

Following code should help you.

 
$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";

 }

Open in new window


Thank You.

Amar.
0
 
Ray PaseurCommented:
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
 
psimationCommented:
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
 
rgb192Author Commented:
Thanks all
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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