Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't use the comparison operators with strings like that, because when you do the strings <a href="http://php.net/manual/en/language.operators.comparison.php" rel="nofollow noreferrer">get converted to numbers first</a>.</p> <p>For an one-liner solution, you can use <a href="http://php.net/manual/en/function.strcmp.php" rel="nofollow noreferrer"><code>strcmp</code></a>:</p> <pre><code>if(strcmp($my_time, date('H:i')) == 1) { do something ... } </code></pre> <p>The condition above is semantically equivalent to "if $my_time is greater than the current time", <em>but only if the format of the strings remains consistent!</em> <strong>It's very easy to introduce a bug in this code</strong> if for any reason the format of <code>$my_time</code> does not directly correspond to the <code>H:i</code> pattern.</p> <p>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 <a href="http://php.net/manual/en/class.datetime.php" rel="nofollow noreferrer"><code>DateTime</code></a> class, introduced in PHP 5.2.0 (John Conde has already given an example in <a href="https://stackoverflow.com/a/10497966/50079">his answer</a>).</p> <p>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. <code>DateTime</code> approaches are dependent on the local timezone and date, and might not always give you the expected results. Example:</p> <pre><code>// 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???"; } </code></pre> <p><strong><a href="http://ideone.com/Qm83s" rel="nofollow noreferrer">See it in action</a></strong>.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload