Expire?

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

Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Expire?

Post by Mr. Tech »

Hi!

I am wanting to write a members script where their subscription expires in a months time. How do I do that? Would it have something to do with time()?

What code would I need? Thanks!
net7
Forum Commoner
Posts: 31
Joined: Wed Mar 12, 2003 1:27 pm

Post by net7 »

Now, that depends. Are you storing the usernames in a database? Do they require passwords? You could use cookies, but that probably wouldn't be the best way to approach it.

When the user signs up you could along with entering the username and password in the database add the date or time. Then if when you have users login you could check to see if it has been 30 days since they first registered. If it has, then delete their account.

Or you could make and admin page and ever time you run it it would check all the users dates are less than 30 days old.

Hope this helps.
craigh
Forum Newbie
Posts: 19
Joined: Sun May 19, 2002 2:50 pm

Post by craigh »

don't delete the account, simply disable it. Create a column in the DB 'enabled' and make it default to 'Y' and after 30 days change it to 'N'. Then check their enabled status before allowing one to log in. I assume that you would want to keep their info around or allow them to re-enable the account for some reason - if not, then delete them I guess. :-)
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Thanks both of you!

I use MYSQL to store their username and password. I am actually adding it to phpnuke. In the dB they have user_regdate and the format for it is Mar 12, 2003. Can i use that at all?
craigh
Forum Newbie
Posts: 19
Joined: Sun May 19, 2002 2:50 pm

Post by craigh »

yeah sure, see the mktime and date functions in the PHP manual at php.net to see how.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

So I would do this:

echo date ("Mar 12, 2003", mktime (0,0,0,12,32,1997));

And that would give me something like:

33213214541201

?

Thanks so much for your help!
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Anyone have any ideas?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

My idea would be to dump the horror that is php nuke and write your own CMS but that's maybe not very helpful.
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

No... not helpful at all.... Don't worry about phpnuke, I just want to work out this code first :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What type of field is user_regdate?

Mac
junrey
Forum Newbie
Posts: 12
Joined: Thu May 09, 2002 9:53 pm
Location: Cebu, Philippines
Contact:

Post by junrey »

Hope that this would help.

One thing that you need to consider is the data type of your user_regdate field. If it is a timestamp field then it would be ok but if it is not, then you need to use mktime function to convert it to timestamp.

To determine the timestamp of the current date, you may try the following code:

Code: Select all

$today = getdate();

// just in case you will consider the time, hour : minute : seconds

$currentTimestamp=mktime($todayї'hour'], $todayї'minute'], $todayї'second'], $todayї'mon'], $todayї'mday'], $todayї'year']);

// then determine the difference between the date of registration and the 
// current timestamp. Supposing that you have the variable 
// $regTimestamp that serves as the timestamp of the registration

$diff = $currentTimestamp - $regTimestamp

// the $diff variable now contains the difference in seconds. so u may 
// need to divide it with 3600 to determine the number of days.
Hope that this helps.

8)
Junrey
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Well, I don't have timestamp so I'll have to use mktime. The date format in the db looks like this:

Mar 12, 2003

What mktime code would I need to use to convert that?

Thanks a heap for your help junrey!

I appreciate it!

Thanks

Ben
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is the date really not in a DATE field? Is it in a VARCHAR field?

Mac
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

VARCHAR. Is that bad?

Thanks for all your help! :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Personally I think storing dates in MySQL in anything other than DATE, DATETIME, TIMESTAMP or as a UNIX timestamp in an INT column is pointless as it leads to truly unneccessary hassle when you try and sort or print these dates.

Anyway, you could use PHP to work out whether or not membership had expired (was 30 days later than subscription date) by doing:

Code: Select all

<?php

$db_date = 'Mar 12, 2003';
$registered_date_timestamp = strtotime($db_date);
$thirty_days = 30*24*60*60;

$now = time();
$expired = $now - $thirty_days;

if ($registered_date_timestamp > $expired) {
	echo 'ok';
} else {
	echo 'expired';
}
?>
Some of the code is redundant but is there to show logic more than anything.

Check out:
http://www.php.net/strtotime
http://www.php.net/time

Mac
Post Reply