11

I have time saved in database like 7:30pm as a varchar field. I want to check if this time is greater than time right now or not.

I converted the DB time string into '19:30' and now I want to do something like this:

$my_time = '19:30';

if($my_time  > date('H:i'))
{
    do something ...
}

The problem is the above will return always true if $my_time is non-empty string.

doing strtotime($my_time) is not helping either.

strtotime('H:i',$my_time) makes it 00:00 .

doing (int)date('H:i') will give 1700 when the actual time is 17:09, so removing colon and then comparing will not work too ....

Changing database time data is out of question in this context.

plz help. Correct me if I stated some facts wrong.

sri_wb
  • 339
  • 1
  • 6
  • 16

5 Answers5

15

You can use this:

$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
    // do something
}
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • 7
    A lot of time has passed, but isn't it the same as directly comparing two strings? Since `date()` returns a string, it is exactly as doing: `if(date('H:i') == '19:30')` isn't it?, which is the same thing the OP mentioned trying. That is because `date('H:i', strtotime($myTime))` returns the original $myTime value as string!!! So it is the same as just puting `$myTime` instead of `date('H:i', strtotime($myTime))`.so what is the point? I really don't get it. What am I missing? Why the `if($my_time > date('H:i'))` the OP suggests supposedly doesn't work? because it DOES work for me. – DiegoDD Aug 05 '13 at 22:17
  • 2
    question involved how to compare if one is greater than second. not equality comparison only. – T.Todua Oct 05 '17 at 19:40
5

You can construct a new DateTime object, setting the time on a random date. Than compare those two objects. eg:

$my_time = new DateTime('January 1th 1970 19:30');
$comparable_time = new DateTime('January 1th 1970 '. date('H:i'));
if($my_time < $comparable_time) {
    // do something
} else {
    // do something else
}

Please take note of the changelog;

Version 5.2.2    DateTime object comparison with the comparison operators changed to work as expected. Previously, all DateTime objects were considered equal (using ==).
giorgio
  • 10,111
  • 2
  • 28
  • 41
2

You can't use the comparison operators with strings like that, because when you do the strings get converted to numbers first.

For an one-liner solution, you can use strcmp:

if(strcmp($my_time, date('H:i')) == 1)
{
    do something ...
}

The condition above is semantically equivalent to "if $my_time is greater than the current time", but only if the format of the strings remains consistent! It's very easy to introduce a bug in this code if for any reason the format of $my_time does not directly correspond to the H:i pattern.

Dumbing down the values to strings is usually not the way you should be going about using dates and times. A more appropriate solution would be to use the native DateTime class, introduced in PHP 5.2.0 (John Conde has already given an example in his answer).

However, there is also one possible advantage to treating times as dumb scalar values: the results are consistent with the human perception that 01:00 is always later than 00:00. DateTime approaches are dependent on the local timezone and date, and might not always give you the expected results. Example:

// assume we are in London
date_default_timezone_set('Europe/London');

// assume that today is March 25, 2012
$date1 = new DateTime("2012-03-25 01:00:00");
$date2 = new DateTime("2012-03-25 02:00:00");

// and...
if ($date1 == $date2) {
    echo "WTF?!? Equal???";
}

See it in action.

The result of this test is different than what comparing some scalar representation of "01:00" and "02:00", so it's a good idea to think about what the proper semantics are for the comparison.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
1
$date1 = DateTime::createFromFormat('H:i', $my_time1);
$date2 = new DateTime();
if ($date1 > $date2)
{
     // do something   
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • +1, and the obligatory disclaimer: The results of this test might be surprising for specific values depending on the current date and timezone. – Jon May 08 '12 at 11:53
  • I am getting the following error: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (H:i) at position 1 (:): Unexpected character' in C:\wamp\www\workspace\bigbite\index_parallel.php on line ...................and also followed by the warning: Exception: DateTime::__construct() [datetime.--construct]: Failed to parse time string (H:i) at position 1 (:): Unexpected character in C:\wamp\www\workspace\bigbite\index_parallel.php on line – sri_wb May 08 '12 at 12:12
1

Don't compare strings which represent timestamps. Instead, use strtotime() to convert any such strings to Unix timestamps, which are just numbers, and then compare these. You can get the Unix timestamp for the current time with time():

$my_time = '19:30';

if (strtotime($my_time) > time()) {
    // do something ...
}
Jivan Pal
  • 182
  • 10