Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,164,188 members, 7,856,751 topics. Date: Tuesday, 11 June 2024 at 06:38 AM

Writing A Time Based Expiration Script Or Function Using Php And Mysql - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Writing A Time Based Expiration Script Or Function Using Php And Mysql (17529 Views)

Bulk Sms Script Or Software / Form Validation Tutorial Using Javascript, Php And Ajax! / Need Script, Or Any Solution For Website Design Task! (2) (3) (4)

(1) (Reply) (Go Down)

Writing A Time Based Expiration Script Or Function Using Php And Mysql by tundewoods(m): 11:49am On Feb 05, 2008
Hi folks,I am working on a real estate web portal that will require users to either sign up and list and manage their properties or search for available houses based on the different criteria,using php and mysql.

Based upon request of the client i am suppose to develope the property listing application with a date & time based expiration function or class.That will automatically expire and deactivate a users property after a specified period of time for example 7 days or 30 days.

From my own understanding of php coding i've attempted to solve the problem by creating 3 extra fields in the property listing table in my mysql database.

the first field will be a ENUM field for listing status that will have default value (Yes,No )or (1,0) while the 2nd extra field is a timestamp field that automatically stores the time property was listed while the 3rd field is also timestamp that calculates and stores the specified expire date of the listing.(I guess or presume that should be expected too)

Now i actually need anyone who's worked on a similar app before to please guide me through the steps to write the function or script so that i can actually use the script to calculate and arrive at the date and time due and automatically run via a CRON or whatever and automatically deactivate the listing by updating the active field from 1 to 0 or Yes to No
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by WebMonk(m): 1:08pm On Feb 05, 2008
I've done something similar (checking if a list of articles are more than 2 weeks old and moving those affected to another table or listed as 'old').however,i did this in access/asp.but the technique should apply as well.
See if mysql has a function using 'time' to carry out specific actions on a table based on a criteria.i did this in access, copied the code and added it as a function in dreamweaver.i'm hoping mysql has the same.hope this helps.i'm using my phone to post this,if i have the time i'll post a link.cheers!
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by my2cents(m): 3:26pm On Feb 05, 2008
tunde,

I haven't implemented this before (but who knows, I might eventually so I might as well figure out how to now tongue)) but this link may have all the answers you seek: http://www.thescripts.com/forum/thread614886.html

It mentions the mysql event scheduler.

I hope this helps.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by kazey(m): 3:59pm On Feb 05, 2008
This is how I would approach the problem, I am bored cheesy

Note: This is an illustration:

Tables
id
User
Date
Duration
Status (Active,Pending,Expired)

Program [CRON]
SELECT date from table where id=''

$date = substr($todays_date, 6, 2);

$trigger_date = date("Ymd", mktime (0,0,0,$month,$date-$number_of_days,$year));

You just needed the calculation right?
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by tundewoods(m): 11:46am On Feb 07, 2008
Hello Anybody out there ?I'm still battling with this app function oh shocked
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by ThePhantom(m): 12:36pm On Feb 07, 2008
use cookies

setcookie("Example", "User", time()+3600); expires in 1 hour

read more at http://us.php.net/setcookie

You will also need a field in you user table that tracks the timestamp when the person login.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by tundewoods(m): 1:13pm On Feb 07, 2008
ThePhantom:

use cookies

setcookie("Example", "User", time()+3600); expires in 1 hour

read more at http://us.php.net/setcookie

You will also need a field in you user table that tracks the timestamp when the person login.

I understand what you are talking about but i doubt if you understand what im trying to do.I am not wrting a function to expire a users login session,rather than that im writing something that will automatically expire users listed item on a database listing once the specified time is elapsed.

Cookies or sessions are not useful here rather a function that calculates between 2 time stamp fields and a CRON to automatically update the database table.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by my2cents(m): 2:49pm On Feb 07, 2008
so, I have been thinking,

In the interest of time (at least for a first pass), why not bypass the CRON altogether? You could do this on the fly in your code:

Say you have the 2 timestamp fields. You do a select * on the mySQL table, then get the 2 timestamps fields. Then either do the calculation using a mySQL function (if it exists. I don't know as I have had no reason to research this) or in your PHP, then run your calculation. If the value exceeds whatever number you set, either you don't display that record or you display it with a "disabled" text or whatever in a "status" column of your HTML table.

Probably not too pretty, but that could buy you time while you figure out your CRON job thingie cool

Make sense?
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by kazey(m): 2:55pm On Feb 07, 2008
lol my illustration should be able to solve your issue if you are a programmer? You require the entire code is it? I can write it for you, but busy at the moment. If you can wait till sunday.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by ThePhantom(m): 6:23pm On Feb 07, 2008
My bad, it was late and I wasn't thnking straight. Here is a simple approach

I would create the table like this(add more fields if you like)

[Table]
id
Property
User
StartDate
EndDate
Duration
Active

Have the CRON file run daily, so it calculates the remaining days and deactivates any listing that has a zero duration
[CRON]
Update table set duration= (EndDate - StartDate)
Update table set Active = 0 where duration = 0
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by kazey(m): 6:25pm On Feb 07, 2008
I got bored and quickly wrote a simple code to do what you want, modify to suit your needs.

Note: I didn't debug it. So if you have any bug, kindly let me know cheesy

id => user id
sdate => the date the counting should begin
days => number of days to expire
status => Client Account status

<?php 


$resultx = mysql_query("SELECT * FROM client WHERE status='Active'"wink
or die(mysql_error());

$x = 0;
while ($datax = mysql_fetch_array ($resultx))
{
$id = $datax['id'];


$days_to_expire = $datax[days];

//should be in the format of "Ymd"
$sdate = $datax[startdate] ;

//todays date
$todays_date = date('Ymd', mktime (0, 0, 0, date ('m'), date ('d'), date ('Y')));

$timeStamp = strtotime("$sdate"wink;
$timeStamp += 24 * 60 * 60 * $days_to_expire ; // (add expirydate days)
$expiry_date = date("Ymd", $timeStamp);

//select information from client where client is due for expiry
if ($expiry_date == $todays_date){
$query = '' . 'UPDATE client SET status=\'Expired\' WHERE id=\'' . $id . '\'';
$result2 = mysql_query ($query);
}



++$x;
}


$report = '' . $x . ' Number of Account Expired';
?>


*my2cents correction applied.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by my2cents(m): 6:45pm On Feb 07, 2008
maaan kazey, see groove! You beat me to it grin

In any event, I think there might be a very minute bug on line 1 - SELECT * FROM client WHERE status='Active' cool
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by tundewoods(m): 4:21pm On Feb 08, 2008

@Kazey
Thanks man for the logical approach you posted.I am still integrating the approach to suite my current project.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by webguru(m): 7:13pm On Feb 13, 2008
thot cron was used to run files

when did it start running queries
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by ThePhantom(m): 7:30pm On Feb 13, 2008
@webguru

It is used to run files. Those were just examples of what you will place in the file that cron will run.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by webguru(m): 6:53pm On Feb 15, 2008
ok i thot it was somethin new

im cool wink
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by changeco: 6:17pm On Feb 25, 2009
I have a similar coding issue,  However, It seems far less complicated,  I have a recordset that returns the startdate and enddate. I want to check the startdate against the end date. If the users enddate has passed, redirect them to an "expired.php" page, if not, continue; in that same function if the startdate is after the current date redirect them to a "notyet.php" page that displays their startdate,

The expired page works, which is strange the notyet page doesn't,

Here's what I have so far,

<?php
$startdate = $row_product['startdate'];
$enddate = $row_product['enddate'];
$todays_date = date("Y-m-d"wink;

if ($enddate <= $todays_date) {
header('Location: expired.php');
}
elseif ($startdate < $todays_date){
header('Location: notyet.php');
}else {
}
?>


Any help would be a better than perfect!
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by yawatide(f): 6:34pm On Feb 25, 2009
I think you are missing a function to be wrapped around your dates: strtotime()

So it would be something like this (pseudocode):
start date = strtotime(start date);
end date = strtotime(end date);
today = strtotime(today);

Then you do your comparisons (again, pseudocoe):
if (end date <= today) then
    do stuff
else if (start date < today) then
    do stuff
else
    do stuff

Note:
1) You can't compare dates directly in "Y-m-d" format (or maybe you can but I just don't know  cool) because of the "-".  At the very least, you would need to apply "explode".  Having said that, I think my approach works better and is more straightforward.  The strtotime() will convert to a unix timestamp which looks like an actual number that makes checking easier

2) you will still need the $todays_date = date("Y-m-d"wink;  part above.  of course the assumption is the dates you are getting from ur dates columns r in the same format. if not, you will need to convert them such that strtotime() can take and convert them.

Finally, thanks to actually searching the archives for this instead of writing a new post. Reading this from top to bottom actually made it easier for me to follow and appreciate.

Good luck!
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by Idealws: 10:07am On May 28, 2009
Hello,

Well this is my first post so i hope this is helpful. I noticed the $x variable is being incremented
by one regardless of if it is a expired add or not. This is fixable by removing the ++$x; and putitng
it in the if() statement like below:


<?php
//select information from client where client is due for expiry
if ($expiry_date == $todays_date){
    $query = '' . 'UPDATE listings SET status=\'Expired\' WHERE id=\'' . $id . '\'';
     $result2 = mysql_query ($query);
     $x++;
}
?>


I added a field to my database called numberdays to set the expire time to a different
one depending on the add. Here is my finished code in case it helps anyone.


<?php
// Lets check to see if any featured ads are expired
$resultx = mysql_query("SELECT * FROM listings WHERE status='Active'"wink;
$x = 0;
while ($datax = mysql_fetch_array ($resultx)) {
$id = $datax['id'];
$days_to_expire = $datax['numberdays'];

//should be in the format of "Ymd"
$sdate = $datax['datestarted'] ;

//todays date
$todays_date = date('Ymd', mktime (0, 0, 0, date ('m'), date ('d'), date ('Y')));

$timeStamp = strtotime("$sdate"wink;
$timeStamp += 24 * 60 * 60 * $days_to_expire ; // (add expirydate days)
$expiry_date = date("Ymd", $timeStamp);

//select information from client where client is due for expiry
if ($expiry_date == $todays_date){
    $query = '' . 'UPDATE listings SET status=\'Expired\' WHERE id=\'' . $id . '\'';
        $result2 = mysql_query ($query);
        $x++;
}
}
$report = '' . $x . ' Number of Account Expired';
?>


Here is my table for this code:

CREATE TABLE IF NOT EXISTS `listings` (
  `id` bigint(20) NOT NULL auto_increment,
  `vid` bigint(20) NOT NULL,
  `sectionid` varchar(20) NOT NULL,
  `datestarted` varchar(cool NOT NULL,
  `numberdays` tinyint(4) NOT NULL,
  `status` varchar(25) NOT NULL,
  PRIMARY KEY  (`id`)
);


I keep my listings in a different table but you get the idea. This way I can have ads
that list for 30,45,90 days if I want.

Hope this helps.

Regards,
Ray
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by RuuDie(m): 4:31pm On May 28, 2009
The PHP keyword "die" - what exactly does it do and what other substitutes can be used in its place?
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by DualCore1: 4:53pm On May 28, 2009
die() ends the execution of a script.
Re: Writing A Time Based Expiration Script Or Function Using Php And Mysql by Nobody: 9:28am On May 30, 2009
die() - php
end - visual basic
stop - qbasic
All of them stops the execution of the program . . . dont confuse die with exit in php. . . just look 'em up in the php manual

(1) (Reply)

14-Year-Old Nigerian Bulk SMS App Developer In A Dilemma / Top Twitter Handles In Nigeria. / Nairaland.com Redirecting To My.Naij.Com On Etisalat?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.