Am having a problem with my last seen time script. its outputting is not normal.

it outputs wrong results, let me say i can set Seen time to current one, it says seen 1 hour ago. yet its just 2 minutes ago

Thanks i appreciate for help.

#!/usr/bin/perl use strict; use warnings; use Date::Parse; use DateTime; use POSIX qw/strftime/; my $Datetime = DateTime->now; my $Seen = '01/27/2022 07:40:03'; my $CurrentDateTime = strftime('%m/%d/%Y %H:%M:%S',localtime); my $SeenDateTime = DateTime->from_epoch( epoch => str2time( $Seen ) ); my $CurrentDate_AndTime = DateTime->from_epoch( epoch => str2time( $Cu +rrentDateTime ) ); my $diff = $CurrentDate_AndTime->subtract_datetime( $SeenDateTime ); my ( $years, $months, $weeks, $days, $hours, $minutes ) = $diff->in_units(qw( years months weeks days hours minutes )); my $SeenTime =''; if ( $years >= 1 ) { $SeenTime = "Seen $years Years Ago"; } elsif ( $months > 1 ) { $SeenTime = "Seen $months Month Ago"; } elsif ( $months == 1 ) { $SeenTime = "Seen $months Month Ago"; } elsif ( $weeks > 1 ) { $SeenTime = "Seen $weeks Weeks Ago"; } elsif ( $weeks == 1 ) { $SeenTime = "Seen $weeks Weeks Ago"; } elsif ( $days > 1 ) { $SeenTime = "Seen $days Days Ago"; } elsif ( $days == 1 ) { $SeenTime = "Seen $days Days Ago"; } elsif ( $hours > 1 ) { $SeenTime = "Seen $hours Hours Ago"; } elsif ( $hours == 1 ) { $SeenTime = "Seen $hours Hours Ago"; } elsif ( $minutes > 1 ) { $SeenTime = "Seen $minutes Minutes Ago"; } elsif ( $minutes == 1 ) { $SeenTime = "Seen $minutes Minutes Ago"; } elsif ( $Seen == $CurrentDateTime) { $SeenTime = "Online"; } print $SeenTime;

In reply to 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.