How to extract only hour data from PHP time string?

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

I have a query that retrieves a bunch of these times from a database, but I only need this link.

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

I studied the explosions and substrings, but did not seem to understand.

Thank.

+3
source share
4 answers

A line burst will look like this (you probably want to add intval()to use it as a real number):

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

, , . , MySQL (HOUR() ):

SELECT HOUR(`time_column`) AS `hour` FROM `your_table`
+9

SELECT TIME_FORMAT(field_name, '%H') as return_hour from table_name
+1
$ time_string = '09: 51: 06 ';
$ your_array [$ array_index] = substr ($ time_string, 0, 2);

for more information about substr

+1
source

I think you can do this either with string manipulation or with time / date. Any suitable date method would be on the SQL side, PHP has mktime and date functions, but you will have the hour you are looking for on the intermediate path, if you still had to build the date

Line

$thisHour = array_shift(explode(':', $timeString));  //this gets hour
$thisHour - substr($timeString, 0, 2);              // or this
$hours[] = $thisHour;                               //this adds to array
0
source

Source: https://habr.com/ru/post/1776930/


All Articles