Ahnaf
Ahnaf

Reputation: 51

calculate time differnece in hours and minutes using php when start time and end time is in 12 hour format

I'm working on hall reservation project and got stuck in calculating time difference in hours and minutes.

Scenario:
Start time = 11:00 AM.
End time = 03:30 PM.
Result = 04:30.

I have tried $diff = $start->diff($end); but it does not work.

Upvotes: 1

Views: 2452

Answers (2)

Fadey
Fadey

Reputation: 430

I would suggest you to use Carbon which is the best package (IMO) for handling dates and time in php

Upvotes: 0

Faisal Hameed
Faisal Hameed

Reputation: 104

You should gmdate() function.

$StartTime= '11:00 AM';
$EndTime = '03:30 PM';
$sst = strtotime($StartTime);
$eet=  strtotime($EndTime);
$diff= $eet-$sst;
$timeElapsed= gmdate("h:i",$diff);

Upvotes: 2

Related Questions