How best to calculate the "intermediate" intervals between two dates

Asked

Viewed 33 times

0

I’m developing a system in Laravel with Fullcalendar v4. When creating an "event", this event has execution date (dtstart) and expiration date (until), however there will be intermediate events, based on the amount of events reported. The question is: how to calculate the intermediate dates between 'dtstart' and 'until', that is, the difficulty is not to encode but to find the correct formula.

Example:

$dtstart = '2019-04-01';
$until   = '2019-05-30';
$eventos = 5;

In this example, I have a difference of 59 days (until - dtstart -1). I need to generate more '3' events/dates, and these events are between 'dtstart' and 'until', so distributed between this given range. Of course these values can be other.

Thank you!

  • 3

    Divide the number of days you have by the number of events minus 1. If you have 59 days for 5 events, the interval will be 59/4. Isn’t that right? Then the rule to round up is up to you.

  • Thanks for the suggestion, frankly, I’m not sure for the simplicity of your answer and even for my scenario... I was working out so many calculations, one more complex than another, that I was already losing it and I thought... it’s not possible not to have a basic calculation for it. But I’ll try again with your model.

  • By sampling, demonstrates to meet the following formula: $diff_days = $dtstart->diff($until, true)->days; round($diff_days / $events);. Again thanks for the suggestion.

  • 1

    It doesn’t need to be by sampling, mathematically it is proven that it will suit you in all situations.

  • Thanks again for the help, without your suggestion, maybe I’d be stuck in complex calculations. Anyway, follow to share what the final solution to my scenario. $diff_days = $until->diff($dtstart, true)->days; round(($diff_days - 1) / ($disparo_quant + 1) ); A small adjustment in the calculation made all the difference.`

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.