Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two times with Perl/CGI

Status
Not open for further replies.

grindange

Programmer
Jul 19, 2004
18
SE
I would like to know how long times a user visit a page. Would like to know the time from when he (or she) login, answer some question and press OK! The times will probably something between 30 seconds up to 3 minutes.

I use HTML, MySQL and Perl/CGI.

Anyone who have some examples or ideas?
 
Do you know how to write perl code? Even if you only know some very basic perl programming this should be quite simple. As long as you have the two events that you can track, 'login' and press the 'OK' button, all you need to do is store/record the time using the time() function for each event then subtract the first time record from the second time record.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I know Perl, but I don’t have very deep knowledge...

More info, I save the in-time and the out-time in MySQL and the problem is when someone have in-time 15:59:20 and out-time 16:01:16. It's even worse if they answer very late the last day of a month and the out-time is a few minutes after, but with a new months.

I guess there is some module I can use or easy script. I can not be the first person with this problem!
 
Use the Perl time() function, it returns the number of seconds that have elapsed since December 31, 1970.

So it returns a number of seconds, which means you don't have to deal with minutes, hours, days, weeks, years, leapyears, or anything complicated like that.

Check it out:
Code:
$ perl
print time(), "\n";

Code:
1187496656

So, 10 seconds later, the number that time() returns will be 10 greater.

So to compare:

Code:
my $start = time();

sleep(15);

my $end = time();

# see how much time has passed
# (should be about 15 seconds)
my $elapsed = $end - $start;
print "$elapsed seconds have passed.\n";

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
OK, save the 'in' time and 'out' time using the time() function or if mysql also can save time in epoch sends just use a mysql command.

Saving t time in epoch seconds makes most things much easier in the long run, and usually in the short run. The small effort it takes to convert epoch seconds into human readable format (when that is needed) is trivial compared to the effort needed to convert human readable time into something you can use to make calculations with.

You should only ever do that when time in epoch seconds is not available for some reason: never store the time in anything but epoch seconds, and if you do, still store the time in epoch seconds somewhere else, because you never know when you may need to go back and do some date/time calculations.

If all you have is the time stored in hours/minues/seconds there is no way to even compare the time difference unless they are always less than 24 hours apart, otherwise all you can know for sure is that there is under 24 hours difference or over 24 hours difference or exactly 24 hours difference.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I would get the time (number of seconds since 1970) and save it to a temporary file. Then once the user has finished get the time again. Compare the times... To check how long they have been, the script will be something like this... You could also store data using cookie rather than file

#! /usr/bin/perl
use strict;
use CGI ':standard';

open (LOG, "Path_To_Temporary_File_Storing_Begin_Time") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
my @time = <LOG>;
close (LOG) || Error ('close', 'file');

my ($expiretime)=time;

my $time2 = @time;

my $total = $time2-$expiretime;

print "The number of seconds this user has been is $total";

 
He's got mysql.. he probably wouldn't want to use a flat file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Ahh okay. There is probably a way to store data into the database. Though I don't know enough, never used my sql with perl, and only a little with PHP. Though creating a temp text file is a simple solution, and could be secured if need be using htaccess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top