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

# 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
1 Solution

Commented:

``````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";
}
``````
0

Commented:
#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

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