Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Carbon: split datetime diff in hours format (by seconds)

I'm looking for a way to split the difference and know how much minutes of the hour is consumed, for example:

Date 1: 2021/06/30 10:20PM
Date 2: 2021/06/30 11:20PM

Expected result:
10 PM: 40min
11 PM: 20min

Another example:

Date 1: 2021/06/30 9:54AM
Date 2: 2021/06/30 1:35PM

Expected result:
9AM: 6min
10AM: 60min
11AM: 60min
12PM: 60min
1 PM: 35min

is there a way for caborn to output in this format? the only thing I've found is the difference in hours

$start  = new Carbon('2018-10-04 15:00:03');
$end    = new Carbon('2018-10-05 17:00:09');
$start->diff($end)->format('%H:%I:%S');
outputs = 02:00:06
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use this....Tested with all your examples

    $d1 = $temp = new Carbon('2021/06/30 10:20PM');
    $d2 = new Carbon('2021/06/30 11:20PM');

    $results = [];
    while ($temp->lt($d2)) {
        $m = 60-$temp->minute;
        if ((clone $temp)->addMinutes($m)->gt($d2)) {
            $m = $d2->minute;
        }
        $results[] = sprintf('%s: %smin', $temp->format('h a'), $m);
        $temp->addMinutes($m);
    }
over 4 years ago · Santiago Trujillo Report

0

echo $start->diffAsCarbonInterval($end)->totalMinutes;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!