ian
ian

Reputation: 12335

extracting hour value from a DATETIME format

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'

Upvotes: 2

Views: 7966

Answers (3)

ian
ian

Reputation: 1675

in php,

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

$hour should contain 13.

Upvotes: 3

fabrik
fabrik

Reputation: 14365

$hour = date("H", strtotime('2009-08-17 13:00:00'));

Upvotes: 7

Greg
Greg

Reputation: 321698

You can use the HOUR() function:

mysql> SELECT HOUR('2009-08-17 13:00:00');
+-----------------------------+
| hour('2009-08-17 13:00:00') |
+-----------------------------+
|                          13 |
+-----------------------------+

Upvotes: 4

Related Questions