Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Date Tracking -- Help !

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Date Tracking -- Help !

Postby robinray » Fri Jul 18, 2003 5:15 pm

I am writing a program inwhich the user signs up for a free online 30-day free trial of a password protected part of a website.

After that, I am requiring a nominal fee ...

I want to track the user all subsequent times they log in and compare their original signup date with the current date and then either let them into the site OR send them to a "buy now" page if they have exceeded their 30 days ...

I can capture the date they sign up through the PHP date function date() but I want to:

1. Add 30 days to the signup day
2. Compare these dates (signup date and current date)
every time they log back in to track how many days
they have left
3. Send them to a "buy it" page when the 30 days has
been exceeded

Of course, once I get items 1. and 2. down, I can use that info to do 3.

Any help would be greatly appreciated !

Thanks in advance ...

Robin Ray
robinray
New php-forum User
New php-forum User
 
Posts: 15
Joined: Fri Jul 18, 2003 4:37 pm

Postby swirlee » Fri Jul 18, 2003 10:24 pm

Here's how I'd do it: Use date('z') to get the day of the year (e.g. today is day 199 of this year) that the user signed up on. Then, every time the user comes back, use date('z') again to get the current day of the year. Subtract the current day from the first day and you'll know how many days have passed.

Pretty simple, right? Well, not quite. What happens if they signed up sometime in December and come back in January? Then when you subtract the current day from the first day, you'll get a negative number! Well, the way to get around this is to compare the number to zero, and if it's less that zero, just add 365.

And there's one caveat: Leap years. Well, I'll leave that one up to you, but here's a hint: date('L') will tell you if a given year is a leap year. Good luck.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Joan Garnet » Sat Jul 19, 2003 5:57 am

I guess you are using a database to keep user information.
You can save the date the user first logged in as
10 September 2000 or whatever and the compare with the actual date:
Code: Select all
echo strtotime ("now") - strtotime ("10 September 2000");


To check if 30 days have passed by:
Code: Select all
<?php
$row["first_day"] = "10 July 2002"; // for the example, replace for your database query

$max_time = 30*24*60*60; //get the seconds
$first_day = strtotime ( $row["first_day"] );
$now = strtotime ("now");

if ( $now - $first_day > $max_time ){ //compare times

   header ("Location: buy_page.php");

}else{

   echo "You are in" ;

}
?>
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Thanks a million !

Postby robinray » Sat Jul 19, 2003 9:08 am

swirlee and Joan -

Thanks a million for your suggestions ! I was stumped on this and I appreciate you folks taking the time to help me out.

You've both been a TREMENDOUS help on this ...

Until next time !! (And I'm SURE they'll be a "next time" for me with this php code! Ha!)

Robin Ray
robinray
New php-forum User
New php-forum User
 
Posts: 15
Joined: Fri Jul 18, 2003 4:37 pm

Postby swirlee » Sat Jul 19, 2003 10:43 am

If you're using MySQL anyway, you might as well use MySQL's built-in Date and Time functions, which put PHP's to shame. The following will do exactly what robinray wants, and it'll handle leap years and such without batting an eyelash:

SELECT ((NOW() - INTERVAL 30 DAY) > first_login) AS expired;

That will return 0 if thirty days have passed or 1 if it's been fewer than thirty days, and can easily be dropped into an existing query.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Reply to MySQL Time and Date Functions ...

Postby robinray » Sat Jul 19, 2003 11:00 am

swirlee -

Yes, I am using a MySQL database to keep track of my customer's data ...

** I WAS WONDERING ** if there was something on the MySQL side that may help me through this challenge ...

I am trying to make this php coding as clean as possible and using the MySQL Date and Time functions as you have shown me here, may do the trick !

Thanks again - ANOTHER THOUSAND TIMES !!

Robin Ray
robinray
New php-forum User
New php-forum User
 
Posts: 15
Joined: Fri Jul 18, 2003 4:37 pm

Postby Joel » Sat Jul 19, 2003 4:52 pm

The date tracking I've used in the past, is Unix timestampts. So in when the user signs up you can do this query.

Code: Select all
INSERT INTO users (datejoined) VALUES (UNIX_TIMESTAMP())


Then when they go to login

Code: Select all
$now = time();
mysql_query("SELECT username, datejoined FROM users WHERE username = $userinputname AND datejoined + 2592000 > UNIX_TIMESTAMP()");
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.