Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, the <a href="http://en.wikipedia.org/wiki/Lexicographical_order" rel="nofollow noreferrer">lexicographical order</a> of strings in the format <code>yyyy-mm-dd hh:ii:ss</code> works as intended. You could even sort dates like that.</p> <pre><code>$dts = array( '1000-01-01 00:00:00', '2010-03-16 21:22:19', '1000-01-01 00:00:10', '1976-03-27 05:55:00', '1976-03-27 05:54:00', '1968-08-21 12:00:00', '2001-01-01 00:00:01' ); sort($dts); foreach($dts as $dt) { echo $dt, "\n"; } </code></pre> <p>prints</p> <pre><code>1000-01-01 00:00:00 1000-01-01 00:00:10 1968-08-21 12:00:00 1976-03-27 05:54:00 1976-03-27 05:55:00 2001-01-01 00:00:01 2010-03-16 21:22:19 </code></pre> <p>But keep in mind that you will get no feedback for strings that are <em>not</em> in the right format. So, if it <em>could be</em> that there are malformed strings you'd better check that, too. If that is not a concern, string1&gt;string2 is ok.</p> <p>edit: You could even let MySQL do the work. If this is better/equal/worse to doing it in php depends on what you're actually trying to achieve. E.g.</p> <pre><code>$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // setting up a sample table with some values $pdo-&gt;exec('CREATE TEMPORARY TABLE foo (id int auto_increment, d datetime NOT NULL, primary key(id))'); $pdo-&gt;exec("INSERT INTO foo (d) VALUES ('1000-01-01 00:00:00'),('2010-03-16 21:22:19'),('1000-01-01 00:00:10'),('1976-03-27 05:55:00')"); $query = " SELECT d, (d&gt;'1910-03-17 12:00:00') as flag FROM foo "; foreach ( $pdo-&gt;query($query) as $row ) { echo $row['flag'] ? '+ ':'- ', $row['d'], "\n"; } </code></pre> <p>prints</p> <pre><code>- 1000-01-01 00:00:00 + 2010-03-16 21:22:19 - 1000-01-01 00:00:10 + 1976-03-27 05:55:00 </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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