Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql date manipulation

Status
Not open for further replies.

beyondflash

Programmer
Sep 4, 2005
8
US
I have a date comming from mysql yyyy-mm-dd I need to be able to distinguish if this date is over 1 year old.

The date is also in an array:
$mydate[$i] = yyyy-mm-dd;

I need a function that will return a true or false if each date in the array is a year old.

Thaks,
Mike
 
select date_field,if(date_field = date_sub(curdate(), interval 1 year),"True",False") as true_false FROM database WHERE ....;

you now receive true_false in your array and it will tell you ;)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I don't want it a mysql function...I need it a php function so I can tell the user that that particular product they purchased is expired (they will have more than one product purchased.)
 
You can evaluate the returned field, (use 1 for true,0 for false)
if($myArray[$i] == "1"){
echo "product expired";
}else{
...
}

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I go tit ... here is what I did:

$mydate= explode("-",$DBDate[$i]);
$year=$mydate[0]+1;
$month=$mydate[1];
$day=$mydate[2];
$expiredate=$year."-".$month."-".$day;
$today = date("Y-m-d");
$days[$i] = floor((strtotime(date("Y-m-d")) - strtotime($expiredate)) / (60 * 60 * 24 * -1) );
if($days[$i] <=0){
$expired[$i]=1;
}else{
$expired[$i]=0;
}


it may be "bulky" but it works.
 
That was inside a for loop with the size of array as a limiter.
 
it also calculates how many days is left before expiration at the same time. And I display that later using days[$i]
 
depending on version of mysql, datediff could do that, saving you lots of programming while building more advanced mysql queries.

horses for courses tho ;) glad you found a solution.

//karv

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top