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

Posted on 2009-04-16
Last Modified: 2012-05-06
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
Question by:angelblade27
    LVL 39

    Accepted Solution


    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

    LVL 84

    Expert Comment

    #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

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now