Sergey Sinkovskiy Sergey Sinkovskiy - 21 days ago 4x
Perl Question

How can I get approximate time passed since some timestamp?

Is there any useful module on CPAN that returns number of biggest fractions of date interval, i.e. return integer number of years/months/days/hours/minutes/seconds between two dates, to use in sentence like "N days ago" or "M months ago"


DateTime and DateTime::Duration are the best modules:

use strict;
use warnings;

use DateTime;
use DateTime::Duration;
use Data::Dumper;

my $dt1 = DateTime->now;
my $dt2 = DateTime->new(
    year => 2001,
    month => 01,
    day => 01

my $duration = $dt1 - $dt2;
print Dumper($duration);


$VAR1 = bless( {
                 'seconds' => 38,
                 'minutes' => 181,
                 'end_of_month' => 'wrap',
                 'nanoseconds' => 0,
                 'days' => 20,
                 'months' => 110
               }, 'DateTime::Duration' );

You can format absolute times and durations using the sister modules DateTime::Format and DateTime::Format::Duration.