Obviously, the first suggestion is to do it in the database. But if you have good reasons not to do that then consider using the core module Time::Piece:

use strict; use warnings; use Time::Piece; use Time::Seconds; use Test::More tests => 2; my $last_update = ('2025-06-30 14:40:26'); cmp_ok days_since ($last_update), '<', 20, "$last_update is less than +20 days ago"; $last_update = ('2025-05-30 14:40:26'); cmp_ok days_since ($last_update), '>', 20, "$last_update is more than +20 days ago"; sub days_since { my $stamp = shift; my $tp = Time::Piece->strptime (substr ($stamp, 0, 10), '%Y-%m-%d' +); my $dayssince = (localtime() - $tp) / ONE_DAY; }

You might want to use gmtime instead of localtime depending on how you are storing the times in your DB.


🦛


In reply to Re: MySQL datetime by hippo
in thread MySQL datetime by joyfedl

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.