Gossamer Forum
Home : Products : DBMan : Customization :

comparing dates to get a number

Quote Reply
comparing dates to get a number
hi
I am struggling with this one.
I have a database which includes 3 fields "current date" "date last visited"
"reward points"
I would like the visitor to be rewarded points based on the difference between the two dates. This is what I have so far to get the difference in dates. This is just a mixture of ideas I found in the forum so I have no idea if it is correct.
Code:
$compare1 = &date_to_unix($rec{'datelastvisited'});
$compare2 = &date_to_unix($get_date);
$total = $compare2-$compare1;
$rec{rewardpoints} = $total

Now assuming this is the code(a big assumption), where would I put it if I wanted this information to be updated as soon as the visitor logged in.
I am a complete perl novice so help is appreciated.

I would like to have
Quote Reply
Re: [carlstevenson] comparing dates to get a number In reply to
I've been thinking about this since I read it yesterday and have more questions. How are you associating the registered user with the db records? When a registered user logs in do they automatically go to their record so that the date last visited is recorded? If so, how are you getting that date without them modifying their record?

Perhaps if you explain the process that you are looking for in detail someone may be able to help you come up with a solution. I'm thinking something similiar has been done but extra fields were stored in the password file as far as updates according to a user's activity?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] comparing dates to get a number In reply to
Hi

I am setting the database up as a kind of virtual pet game for a school website. Ive got everything working but would like to have the pet appear to have developed while the user is away. So far the user can add food etc using modify_record forms and see how old their pet is and when they last visited.

I dont think having the dates update during login is strictly necessary but could be updated after the login with a modify_record form including hidden fields for the dates. At the moment I have modified the cgi file as follows
Code:

sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$in{'food'} = $in{'food'} + $in{'addfood'};


$status = &validate_record; # Check to make sure the modifications are ok!

The fields are set up as follows
Code:
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
body => [1, 'alpha', 0, 3, 0, '', ''],
arm => [2, 'alpha', 0, 3, 0, '', ''],
leg => [3, 'alpha', 0, 3, 0, '', ''],
head => [4, 'alpha', 0, 3, 0, '', ''],
Userid => [5, 'alpha', -2, 15, 0, '', ''],
food => [6, 'numer', 5, 8, 1, '20', ''],
addfood => [7, 'numer', 5, 8, 0, '20', ''],
happiness => [8, 'numer', 5, 8, 0, '20', ''],
weight => [9, 'numer', 5, 8, 0, '20', ''],
date => [10, 'date', 15, 18, 0, &get_date, ''],
currentdate => [11, 'date', 15, 18, 0, &get_date, '']

);

So at the moment the "food" field is updated when food is added during a session which works fine. I have also put the following into html.pl

Code:
sub html_record_form {


my (%rec) = @_;
my (%rec) = @_;
if ($in{'modify_form_record'}) {
$rec{'date'} = $rec{'currentdate'};
$rec{'currentdate'} = &get_date;

which also works fine and swaps the dates so that the date modified is kept in the date field and the current date is added to the currentdate field

so what I thought was, if I could calculate the difference in the dates I could make it appear to the user that the food had decreased or increased since they last logged in.
if I added hidden fields to a seperate modify_record form then i could get the user to click on a button which calculates the dates and adds food to the food field as below. That way they would log in first, reach a page with a button, which updates the fields then takes them to their record. That way I think the code can be left in the modify_record section?

Code:

sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$compare1 = &date_to_unix($rec{'date'});
$compare2 = &date_to_unix($rec{'currentdate'});
$total = $compare2-$compare1;
$in{'food'} = $in{'food'} + $in{'addfood'} + $total;


$status = &validate_record; # Check to make sure the modifications are ok!

I hope this makes sense, I have no experience with perl (just a designer) so am guessing the code based on other posts -which is one way to learn.

thanks
carl

Last edited by:

carlstevenson: Nov 4, 2002, 1:25 PM
Quote Reply
Re: [carlstevenson] comparing dates to get a number In reply to
Ive had a breakthrough!

I changed the following code from $rec to $in. and it works! I also divided the total by 86400 to get back to days since last visit.

Code:
sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$compare1 = &date_to_unix($rec{'date'});
$compare2 = &date_to_unix($rec{'currentdate'});
$total = $compare2-$compare1;
$in{'food'} = $in{'food'} + $in{'addfood'} + $total;

to

Code:
sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$compare1 = &date_to_unix($in{'date'});
$compare2 = &date_to_unix($in{'currentdate'});
$total = $compare2-$compare1;
$in{'food'} = $in{'food'} + $in{'addfood'} - ($total/86400);

See, i'm learning.
carl

Last edited by:

carlstevenson: Nov 4, 2002, 3:15 PM
Quote Reply
Re: [carlstevenson] comparing dates to get a number In reply to
Great!! I'm glad you are getting things to work!

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/