12

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.

3
  • You should be using the native datetime fields instead of varchar. It would have solved this problem for you. Commented May 8, 2012 at 11:44
  • stackoverflow.com/questions/961074/… Commented May 8, 2012 at 11:46
  • i know John... that is the biggest problem. Commented May 8, 2012 at 11:50

5 Answers 5

15

You can use this:

$myTime = '19:30'; if (date('H:i') == date('H:i', strtotime($myTime))) { // do something } 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
question involved how to compare if one is greater than second. not equality comparison only.
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 ==). 

Comments

3

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.

1 Comment

Its working ...with the India,Kolkata timezone. I guess i can use it on live server.
1
$date1 = DateTime::createFromFormat('H:i', $my_time1); $date2 = new DateTime(); if ($date1 > $date2) { // do something } 

2 Comments

+1, and the obligatory disclaimer: The results of this test might be surprising for specific values depending on the current date and timezone.
I am getting the following error: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: 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() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string (H:i) at position 1 (:): Unexpected character in C:\wamp\www\workspace\bigbite\index_parallel.php on line
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 ... } 

1 Comment

This is the correct answer. Others do not work with > or <

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.