Reputation: 2780
Can someone help and give a solution to compare two times which are in the following format when received: 12:20 AM
.
I have two time fields in my script which are meant to be stored in the database after validation, but after validation that the format is in correct order i want to do comparison between those two fields.
The comparison i want to do is to check if the second field not overlapping the first one.
For Example:
The first variable is named
$timeStart
, and holding the time value of = "05:30 PM".The second is named
$timeEnd
, holding a value of = "4:00 PM".
Now, i want to do a comparison to see if the $timeEnd is before $timeStart (also putting into consideration AM/PM when comparing) if it is i dont want to echo an error, otherwise continue with the script.
I'm doing it for a Event Script, so timing has to be right :) $timeEnd has to be after $timeStart.
I sure do hope i explained it clear enough :( Thanks for taking the time to read my complicated gibberish :)
Upvotes: 1
Views: 3291
Reputation: 14328
This should help
function timediff($start, $end)
{
if($end >= $start)
{
return (round(($end-$start)/3600, 0))." Hrs ".((($end-$start)%3600)/60)." Min";
}
return false;
}
echo timediff(strtotime('yesterday 5 pm'), strtotime('today 4:30 am'));
Upvotes: 2
Reputation: 26451
if(function greaterDate($start_date,$end_date)
{
$start = strtotime($start_date);
$end = strtotime($end_date);
if ($start-$end > 0)
return 1;
else
return 0;
}
$time = greaterDate($start_date,$end_date))
Upvotes: 2
Reputation: 522461
if (strtotime($timeEnd) < strtotime($timeStart)) {
// fail
}
Only makes sense though if you're not going to allow day crossovers, obviously.
Upvotes: 1