Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

Get the difference in mintues between a given date/time and the current date/time

I have a date/time in the format
2009-04-16 11:27:04 and i want to get the difference between that time and the current time to see the time elapsed is less than or equal to 60 minutes
0
angelblade27
Asked:
angelblade27
1 Solution
 
Adam314Commented:

use Time::Local;
 
my $datetime_str='2009-04-16 11:27:04';
if($datetime_str =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) {
	my $datetime = timelocal($6,$5,$4,$3,$2-1,$1-1900);
	my $delta = (time() - $datetime)/60;
 
	print "delta = $delta minutes\n";
}

Open in new window

0
 
ozoCommented:
#another method could be
use POSIX qw(strftime);
my $datetime_str='2009-04-16 11:27:04';
if(  strftime("%Y-%m-%d %H:%M:%S",localtime time-60*60) le $datetime_str  ){  
   print "the time elapsed is less than or equal to 60 minutes\n";
}
#both methods could have a problem near a daylight saving time switch
#but there may be nothing you can do about that, because if daylight saving time can apply, your $datetime_str could be inherently ambiguous
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now