简体   繁体   中英

Obtain Carbon difference between two dates in 15 minute increments

The following code will return the difference between two Carbon dates in minutes:

$carbonNow = Carbon::now();
$diff = $carbonNow->diffInMinutes($someRandomFutureDateVariable);

Which will return a single minute result. But I'm trying to get the difference in a 15 minute increments - such as every timestamp for every 15 minute increment in the result. Pseudo code would be:

[
    '2020-10-15 12:45:00',
    '2020-10-15 13:00:00',
    '2020-10-15 13:15:00',
    '2020-10-15 13:30:00',
    ...
]

In addition, I'd like to be able to set the time difference, so if I didn't want to target a 15 minute increment I could target whatever increment I pass into my function. I'm able to get the difference between two dates, but stuck on returning timestamps per each increment.

carbon::diffInMinutes delivers the minutes and removes the seconds.

The minute difference for a given minute increment can easily be calculated as follows:

$inc = 15;  //15 minutes

$carbonNow = Carbon::now();
$diff = $carbonNow->diffInMinutes($someRandomFutureDateVariable);

$diffInc = $diff - $diff%$inc;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM