Home > other >  Laravel PHP Conditioning between 2 Time h:iA format
Laravel PHP Conditioning between 2 Time h:iA format

Time:12-27

I want to condition between 2 h:iA formats but it gives me the wrong results for example.

8AM > 5PM = true (Correct)
8PM > 5AM = false (Correct)
12PM > 8PM = true (Correct)
5PM > 5AM = true (Incorrect)
8AM >11AM = false (Incorrect)

What I want to achieve is the results below,

8AM > 5PM = true
8PM > 5AM = false
12PM > 8PM = true
5PM > 5AM = false
8AM >11AM = true

The end goal is when it gives me false I want the 2 time to beb like this

January 1, 2022 08:00PM - January 2, 2022 5:00AM (If condition is false)
January 1, 2022 08:00AM - January 1, 2022 5:00PM (If condition is true)

But I know how to do this, just want to have a solution about the conditioning since its inconsistent.

CodePudding user response:

Please compare two times in this way:

$from = date("H:i:s", strtotime("10:00:00"));
$to = date("H:i:s", strtotime("21:00:00"));

$result = $from > $to;

CodePudding user response:

I don't know your code but you can compare the data of date first before format it to what you want. here is the example

$start = Carbon::now();
$end = Carbon::now();
if($start > $end){
   $start->format('g:iA');
}
  • Related