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!

time difference 2

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all,

I am trying to find a time difference of 12 hours. When a users enters a value place a time stamp with it. Next time when the user tries to enter a value i check whether the timestamp has exceeded the 12 hours with respect to current time.

To do this i tried something like this ..

###
## Capture current time

$time=time();
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($time);

$year += 1900;
$month=$mon + 1;
if($month < 10)
{
$month='0'.$month; #to display 02 instead of 2
}


if($min < 10)
{
$min='0'.$min;
}

#concat all time values
$today= $year.$month.$mday.$hour.$min.$sec

$timestamp = '1111065753' # this time stamp is generated from time() function eariler when the user sets the value

##################
###
### old time
###
##################

($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1) =localtime($timestamp);


$year1 += 1900;
$month1=$mon1 + 1;
if($month1 < 10)
{
$month1='0'.$month1;
}

if($min1 < 10)
{
$min1='0'.$min1;
}


$old_time=$year1.$month1.$mday1.$hour1.$min1.$sec1;
#-------------------------------------------

To find the 12 hour difference

$diff_time=$today-$old_time;

outputs $diff_time as 11688 ..now i am don't know how to calculate how many hours is this ...

Is there any other easier to find 12 hour time difference.

Could some one shed some light , Thank you.









 
basic math skills is all that is required:

60 (secs) x 60 (mins) x 12 (hours) = seconds in 12 hours

if you have the timestamp in raw format:

$timestamp = '1111065753'

you can compare current time this way:

Code:
if (time >= (60*60*12)+$timestamp) {
   print "It's been twelve hours";
}
else {
   print "It's been less than tweleve hours";
}

I am sure there are already modules for doing these basic date/time type of comparisons but its easy to code a small sub routine just for this specific purpose.
 
How about
Code:
if ( $diff_time/3600 > 12 ) { # 3600 seconds in an hour
    print "i$diff_time is more than 12 hours.\n";
} else {
    print "The time is less than 12 hours.\n";
}

Derek
 
Thanks Kevin, I agree its easy to code sub routine just for this purpose.

I am bit confused here sorry about my 300mhz brain.
Is the output from time function is in seconds ? why adding time function value again to 12 hours seconds.

I wrote it something like this
-------------------------
$oldtime=time();

# captured when users enters a value.
--------------------------------------------
Code:
$time_now=time();
if($time >=(60*60*12)+$oldtime)
 {
    print "Its been twelve hours";
  }
else {
   print "Its been less than twelve hours";
 }
will this also works if the user comes to check after couple of months even years.

Thank you . Bare with my stupid questions.


 
yes, time is output in seconds from a specific time in the past know as the epoch, for most systems its 00:00:00 UTC, January 1, 1970.

You don't need to store time in a scalar unless you will be using that scalar more than once in the script. But say you did use $time_now=time(), than you just put that in the condition:

if($time_now >=(60*60*12)+$oldtime) {
.....
}

time is a function that returns a value without any arguments so you can use time instead of time().

Of course you can also just use the product of 60*60*12:

if(time >= $oldtime+43200) {
.....
}

There are no stupid questions, only stupid answers. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top