views:

123

answers:

3

How would I get the hours from a DATETIME format variable.

Example: 2009-08-17 13:00:00

And I just need to get '13'

+4  A: 

You can use the HOUR() function:

mysql> SELECT HOUR('2009-08-17 13:00:00');
+-----------------------------+
| hour('2009-08-17 13:00:00') |
+-----------------------------+
|                          13 |
+-----------------------------+
Greg
Also see the reference manual for all Date and Time functions: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Mr. Smith
+1  A: 

in php,

list($date, $time) = explode(' ', '2009-08-17 13:00:00');
list($hour, $min, $sec) = explode(':', $time);

$hour should contain 13.

ian
Thanks... perfect except you want to explode $time in the second list();
ian
For a fixed format like this (4-digits, 2-digits, 2-digists etc.) you might as well just use `substr` - as in `$hour = +substr($datetime, 11, 2);`
searlea
+3  A: 
$hour = date("H", strtotime('2009-08-17 13:00:00'));
fabrik