Vijaya Savant
Vijaya Savant

Reputation: 135

php code to check greator than between two given time (e.g. 01:45:00 pm and 11:12:18 am )

In MySQL db , I am storing values of time in "01:45:00 pm" format. Now I have to check whether current time is greator than database stored time. I have tried for:

<?php

$count = 0;

if('01:45:00 pm' >= '11:12:18 am')
{
    $count++; //count is variable
}

echo $count;

?>

It's giving result as 0; So please help me.

Upvotes: 0

Views: 49

Answers (2)

Dezza
Dezza

Reputation: 1094

Use objects rather than strings for your datetimes

<?php
$count = 0;
$time_1 = new DateTimeImmutable('01:45:00 pm');
$time_2 = new DateTimeImmutable('11:12:18 am');
if ($time_1 >= $time_2) {
    $count++;
}
echo $count;

Upvotes: 2

Amrinder Singh
Amrinder Singh

Reputation: 5492

Just need to use strtotime:

<?php
$count=0;
if(strtotime('01:45:00 pm') >= strtotime('11:12:18 am'))
{
$count++;//count is variable
}
echo $count;
?>

Upvotes: 1

Related Questions