in reply to MySQL datetime
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.
🦛
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: MySQL datetime
by joyfedl (Acolyte) on Jul 01, 2025 at 14:40 UTC | |
by hippo (Archbishop) on Jul 01, 2025 at 14:58 UTC | |
by joyfedl (Acolyte) on Jul 01, 2025 at 15:21 UTC | |
|
Re^2: MySQL datetime
by ikegami (Patriarch) on Jul 01, 2025 at 15:14 UTC | |
by hippo (Archbishop) on Jul 01, 2025 at 17:27 UTC | |
by ikegami (Patriarch) on Jul 01, 2025 at 21:55 UTC |