expiring dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

expiring dates

Post by irealms »

i have a quote system and i want to expire quotes after 5 days. So when a user loads their quote page it will check each quote and any 5 days old it will change to archive status.

I can do most of it but how do i check the dates?

I need to check a date value called from the database and if it is more than 5 days before todays date it will run a query.

I have tried subtracting dates but as some months are different this doesn't seem to work.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?
//THIS STUFF IS STORED IN THE MYSQL DATABASE
//Add this information to the db however you want but do not include this stuff in your file.
echo "<form action="{$_SERVER['PHP_SELF']}"\n"
	."<p>Please enter the quote<br/>\n" 
	."<input type="quote" name="location" cols="40"><br>\n"
	."<input type="submit" name="submitquote" value="SUBMIT" /></p>\n" 
	."</form>\n";

$date= date("M-d-Y");
$currentdate = curdate();
$currentdate1 = explode(""-",$currentdate"]); 
$currentdate2 = $currentdate1[1];

//checking days per month
if ($currentdate1[0] == 01) || ($currentdate1[0] == 03) || ($currentdate1[0] == 05) || ($currentdate1[0] == 07) || ($currentdate1[0] == 08) || ($currentdate1[0] == 10) || ($currentdate1[0] == 012){
$monthtotal = 31;
$var=1;
}elseif ($currentdate1[0] == 04) || ($currentdate1[0] == 06) || ($currentdate1[0] == 09) || ($currentdate1[0] == 11) {
$monthtotal = 30;
$var=2;
}else{
$monthtotal = 29;
$var=3;
}

if ($submitquote == "SUBMIT") { 
  $sql = "INSERT INTO quotes SET currentdate2='$currentdate2', `monthtotal`='$monthtotal'";
if (@mysql_query($sql)) { 
    echo("<div align="center">Your Event has been added.<br><br></div>"); 
  } else { 
    echo ("<div align="center">Error adding submitted Event: " . mysql_error() . "</div>"); 
  } 
}

//THIS STUFF WILL BE IN YOUR LOOP
while ($row = mysql_fetch_array($result)) { 

   $currentdate2 + 5 = $enddate;

if (($monthtotal - $endate) > 23) && (var==1)) || ($enddate==32 || $enddate==33 || $enddate==34 || $enddate==35 || $enddate==36) {
$sql = "DELETE FROM quotes WHERE id=$deletequote"; // not sure how you are storing quotes so  I'll leave this sql statement the way it is.
}elseif (($monthtotal - $endate) > 24) && (var==2)) || ($enddate==31 || $enddate==32 || $enddate==33 || $enddate==34 || $enddate==35){
$sql = "DELETE FROM quotes WHERE id=$deletequote";
}elseif (($monthtotal - $endate) > 21) && (var==3)) || ($enddate==33 || $enddate==34 || $enddate==35 || $enddate==36 || $enddate==37){
$sql = "DELETE FROM quotes WHERE id=$deletequote";
}else{

echo "$quote";
}
}
?>

None of this code has been tested at all. This is mainly to get your started ( if it doesnt work ). I'm sure there are a few errors I sorta rushed this.
GL with your project.

Okay, this is just a long shot, I have no idea if it is going to work, but I am finding this script very useful, as I have a need for something like this aswell. So if you end up making this work please post your code.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

select something from table where dateinsert<now()-interval 5 day
//or
update table set status='archive' where dateinsert<now()-interval 5 day and owner='$userId'
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

well i just got pwned.....

:) but does urs compensate for the different days in a month?
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

thanks, i did find this which also compensates for months:

Code: Select all

<?php
$datev = date('Y-m-d');

$newdate = date("Y-m-d",strtotime($datev) - (1 * 24 * 60 * 60));

?>

but that just allows me to work out 5 days from the database value for the date. I then have to work out how to test if the date 5 days from the quote date has passed.

If weirdans code compensates for months then i'm sorted :)
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

seems to work great so far :)
Post Reply