Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract only Hour data from PHP Time string?

Tags:

php

mysql

I have a collection of time records in a database in the format '09:51:06' (hour, minute, second).

I have a query which retrieves a bunch of these times from the database, but I only care for the Hour reference.

How can I get rid of the rest of the string and ad just the hour into an array? So in the example above, I just want to keep '09'.

I've looked into exploding and substrings, but can't seem to figure it out.

Thanks.

like image 355
Jack Roscoe Avatar asked Oct 28 '25 14:10

Jack Roscoe


1 Answers

Exploding the string would look like this (you probably want to add intval() to be able to use it as a real number):

$hours = array_shift(explode(':', '09:51:06'));

But you are probably looking for the right query instead of doing this afterwards. If you are dealing with a time-type, you can use MySQL's date and time functions (HOUR() in this case):

SELECT HOUR(`time_column`) AS `hour` FROM `your_table`
like image 60
jwueller Avatar answered Oct 31 '25 04:10

jwueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!