Jump to content

$datetime->elapsedTimeStr() not showing months and years


dragan
 Share

Recommended Posts

I tried out the new $datetime->elapsedTimeStr() method, and found something strange...

// https://processwire.com/blog/posts/pw-3.0.130/#new-datetime-gt-elapsedtimestr-method
// https://processwire.com/api/ref/wire-date-time/elapsed-time-str/
$p = $pages->get(11297);
$p->of(false);
$start = $p->proj_timespan_from;
$end = $p->proj_timespan_to;
//echo "$start / $end <br>"; // unixtime
$duration = $datetime->elapsedTimeStr($start, $end, true, $options = array("exclude" => ['seconds', 'minutes', 'hours', 'days']));
echo $duration; // shows: 161 weeks. Months and years are not shown :-(

$duration shows: 161 weeks. Months and years are not shown / calculated. Is that by design? I would expect that if the $duration is over a year, it would show x years, y months, z weeks, with my excluded options...

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
On 4/22/2019 at 12:03 AM, dragan said:

Is that by design?

I think the reason might be that a week, day, hour and minute is a fixed number of seconds whereas a month or a year is a variable number of seconds. So from a given number of seconds you can't accurate calculate if a month or year has passed without knowing which month (e.g. February or March) in which year (leap year or common year).

Having said that, the elapsedTimeStr() method does get supplied all the information it would need to accurately calculate elapsed months and years, it just doesn't currently take account of the actual calendar dates and simply uses the difference in seconds.

For now you could use Carbon to get an accurate relative time string including months and years.

2020-06-19_103359.png.e30490ac8f4d58f1870cc17bbc428f30.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...