I would recommend you don't mix POSIX's strftime, Date::Parse, and DateTime. Since you're already using DateTime, you can use its friends DateTime::Format::Strptime for parsing and DateTime::Format::Human::Duration for output.

use warnings; use strict; use DateTime; use DateTime::Format::Strptime; use DateTime::Format::Human::Duration; my $strp = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y %H:%M:%S', on_error=>'croak', time_zone=>'UTC' ); my $durfmt = DateTime::Format::Human::Duration->new(); my $now = $strp->parse_datetime('01/27/2022 07:40:03'); my @tests = ( '01/26/2021 07:40:00', '03/20/2021 07:40:00', '12/20/2021 07:40:00', '01/23/2022 07:40:00', '01/26/2022 07:40:00', '01/27/2022 06:40:00', '01/27/2022 07:15:00', '01/27/2022 07:39:00', '01/27/2022 07:40:03', '01/27/2022 07:40:06', ); for my $t (@tests) { my $seen = $strp->parse_datetime($t); print $now->strftime('%m/%d/%Y %H:%M:%S'), " -> ", $seen->strftime('%m/%d/%Y %H:%M:%S'), " = ", $durfmt->format_duration_between($now, $seen, significant_units => 1, future => 'in %s', past => '%s ago', no_time => 'online now', ), "\n"; } __END__ 01/27/2022 07:40:03 -> 01/26/2021 07:40:00 = 1 year ago 01/27/2022 07:40:03 -> 03/20/2021 07:40:00 = 10 months ago 01/27/2022 07:40:03 -> 12/20/2021 07:40:00 = 1 month ago 01/27/2022 07:40:03 -> 01/23/2022 07:40:00 = 4 days ago 01/27/2022 07:40:03 -> 01/26/2022 07:40:00 = 1 day ago 01/27/2022 07:40:03 -> 01/27/2022 06:40:00 = 1 hour ago 01/27/2022 07:40:03 -> 01/27/2022 07:15:00 = 25 minutes ago 01/27/2022 07:40:03 -> 01/27/2022 07:39:00 = 1 minute ago 01/27/2022 07:40:03 -> 01/27/2022 07:40:03 = online now 01/27/2022 07:40:03 -> 01/27/2022 07:40:06 = in 3 seconds

In reply to Re: time passed calculation by haukex
in thread time passed calculation by frank1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.