• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

How to handle time with CGI

gazz

New Member
I save the time the user last loggs in and compair it to the time the user has currently logged in at.
I save 10 digits, the month, the day, the hour and the minute. If any field is less than 10 I put a 0 infront of it. So say it was November 5th, 11:30. I save 11051130

My question is HOW do I subtract the current time from the last time logged in? A straight subtract doesnt work cause the time being in 60th for minutes and 24 for hours gives strange results.

Anyone have any ideas??
 
My first thought would to be to create two hash tables for the two times, then subtract them field by field...

Anyway you cut it, I think you have to split up the number, subtract, then recreate the time...

The other obvious solution is to modify the way you represent your time. Most programming languages use seconds since 1976...if you do it this way, you can do a straight subtract. The code that converts this to a real time should be pretty simple...
 
Thats an interesting idea. Why do they start at 1976 though? Wont that leave a very very long number? I dont know how perl handles its varialbes yet.. does it have a long equiv for this?

HOw would I do it seperatly? could i do that? Also.. how do I split a string up without using a pipe or whatever? Can you split it up in a certain size chunks? Im asking cause I am unclear how I seperate the time once Ive saved it in the previous format... Or is there a better way to save it?
 
Actually, its 1970...sorry... I have no idea why they chose this date, it just sounded like a good idea at the time (back in 1970) hehe... :D

PERL doesn't care about variable types so a string is easily converted into an integer, and vice versa...

To get certain portions of the string, just do this:

$new = substr($thestring,starting_point,how_many_digits)

Then repeat to get individual portions...

Hope this helps, and as always, good luck! :)
 
Perl

If you're using Perl, then all the functions necessary are already built in.

Store the time as cds mentioned as seconds from 1970 (the Unix epoch!) This is easy! The time() function returns the current time, in seconds since 1970. Again, like cds said, then we can just subtract times like numbers.

#### Begin Perl code ###################

# Time of hit in seconds
$time_of_hit = time();

# Wait a few seconds
sleep(5);

# Find elapsed time
$elapsed_in_seconds = time() - $time_of_hit;
$elapsed_in_minutes = int($elapsed_seconds / 60);

#### That's all for finding elapsed time.
#### The rest of this code converts the $time_of_hit to
#### a human-readable date!

# Use localtime() to convert to Months, Day, etc...
($min,$hour,$mday,$mon,$year) =
(localtime($time_of_hit))[1,2,3,4,5];

# Add 1 to Month because it counts 0..11 not 1..12!
# Add 1900 to Year to get 2000, not 100!
$mon++;
$year += 1900;

# Use sprintf to put in appropriate leading zero's, etc.
$text_time = sprintf ("%04i-%02i-%02i %02i:%02i",
$year, $mon, $mday, $hour, $min);

# Print out results!
print "Hit Time: $text_time\n";
print "Elapsed Seconds: $elapsed_in_seconds\n";
print "Elapsed Minutes: $elapsed_in_minutes\n";


#### End Perl Code #################


Hope that helps,
Adam Ellsworth
GetWebspace.com
admin@getwebspace.com
 
THANKS everyone who responded. Im using your method Adam, I am not sure if it works 100% yet but I am confident it will. And thanks for that code snipet, I didnt realise there was an int() converter as in C in Perl too. I was unsing the sprintf( etc etc to limit my decimal places.

Ill be back soon with onather question :)
 
Back
Top