Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j_watson's avatar

Using Comparison Operators with Time

I have a table data and I want to change it's font-color: green when on time and font-color: red when late (08:00 and 12:00 respectively).

I have this chunk of code but it only changes the font-color to green even when the $data->login is greater than 08:00.

data->login is in datetime format

<td  style="color: <?php $format = 'H:i';
                                                $start='08:00';
                                                $end='12:00';

                                                $login=DateTime::createFromFormat($format, $data->login); 
                                                $min=DateTime::createFromFormat($format, $start);
                                                $max=DateTime::createFromFormat($format, $end);
                                                
                                                if($login <= $min){echo 'green';}elseif($login > $min){echo 'red'; } ?>">
                                               <b><?php $date = date_create($data->login); echo date_format($date, "h:i A - M. d") ?></b></td>
0 likes
13 replies
Snapey's avatar

whats the purpose of $max - you don't use it?

what color when its after 12:00?

j_watson's avatar

@Snapey I'm gonna use $max to determine who logged out too early. I'll do that one after finishing this. Even if it's over 12:00 it still turns green.

tykus's avatar

That is some really ugly HTML; why are you writing all of the PHP within an inline style attribute? What does $data->login look like? What is the purpose of creating the $min and $max variables if you chose instead to use the strings $start and $end for comparison? Is this a Laravel project?

j_watson's avatar

@tykus honestly i think so too. I'm self-taught so codes are just messy.

$data is from a select all query

About $min and $max, I was trying out stuff, forgot to revert it back.

tykus's avatar

@j_watson that doesn't answer the about the nature of login - can you show an example of this piece of data?

j_watson's avatar

@tykus

        +"id": 4
        +"user_id": 4
        +"login": "2023-06-27 07:56:19"
        +"logout": "2023-06-27 10:23:04"
        +"created_at": "2023-06-27 01:56:19"
        +"updated_at": "2023-06-27 01:56:19"
Snapey's avatar

what format is $data->login ?

You use it inconsistently

Snapey's avatar
Snapey
Best Answer
Level 122
<td style="{{ Carbon\Carbon::parse($data->login)->hour() < 8 ? 'green' :  'red'}}" >
<strong>{{ Carbon\Carbon::parse($data->login)->format('h:i A - M. d') }}</strong>
j_watson's avatar

@Snapey it says Object of class Carbon\Carbon could not be converted to int

tykus's avatar

@j_watson it should be hour, not hour()

Carbon\Carbon::parse($data->login)->hour < 8
1 like
Snapey's avatar

@tykus thanks. I was in bed at the time, looking on my ipad and couldn't summon the energy to dig through the awful Carbon docs

1 like

Please or to participate in this conversation.