Get number of days in [report.daterange]

Hi,

Apologies in advance if this question is basic, I’m sure I’m overlooking a straightforward solution.

I’m trying to create a custom template for Pro Reports and have successfully managed to alter the default template for most of my needs. However, I’m struggling to locate or extract a value for the total number of days that the report covers.

For example, for a report covering last month, [report.daterange] token returns ‘August 1, 2024 - August 31, 2024’ which is obviously correct, but I would like to somehow use this information to generate the value ‘31’ for total days in the report for use elsewhere in the template. When next month rolls around, the token will return ‘September 1, 2024 - September 30, 2024’ and I’d like to produce the value ‘30’ for total days, and so on…

I’m not seeing an independent report start or end date anywhere and am unable to manipulate the daterange token to achieve the goal. Is there a way I can accomplish the above that I’m missing? Any help would be greatly appreciated!

Thanks, Scott

Hey @ScottB

Welcome to the MainWP community.

I’ll check with the development team and update you when I have more info.

Hey @bojan ,

Thanks for the welcome and the quick response. I’ve made the switch to MainWP over the last couple of weeks and am really enjoying using it so far - your support in other topics has been very helpful several times already!

Thanks for checking in to this further, looking forward to finding a solution hopefully.

1 Like

Glad to hear you’re enjoying MainWP!

The team has provided this code snippet, which should calculate the number of days of the report:

$datetime1 = new DateTime( date('Y-m-d H:i:s', $report->date_from ) ); // start time
$datetime2 = new DateTime( date('Y-m-d H:i:s', $report->date_to ) ); // end time
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d');
1 Like

Absolutely superb! Tested with a variety of date ranges and working perfectly. Big thanks to you and the team for the quick solution, top notch.

One very minor adjustment for anyone else stumbling across this topic looking for the same answer. To include start and end date in the calculation, create one further variable:

$datetime1 = new DateTime( date('Y-m-d H:i:s', $report->date_from ) ); // start date
$datetime2 = new DateTime( date('Y-m-d H:i:s', $report->date_to ) ); // end date
$interval = $datetime1->diff($datetime2);
$days =  $interval->format('%d') + 1; // Add 1 to include the end date
echo $days;
2 Likes

Cool snippet, thanks, I’ll use it in my reports too. If the date range is bigger than a month, the %d not working correctly, but the %a works good:

$days = $interval->format(‘%a’) + 1;

3 Likes

Of course, even better! Thanks.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.