简体   繁体   中英

Save the date time to database and query the hours only

I need you help. I have a Database then the database i have a date time to save it. this is the format

date("d MYH:i:s");

and this is the output when i query from the database.

30 Jun 2017 03:33:52

My problem is, i want to query only hours only without date how do i do that? somebody help me? thank you.

03:33:52

Thanks in advance

You don't want the hours, you want the time. And MySQL has a convenient function for that:

select time(datetimecol)

If you want to format the column as a string, then you would just do:

select date_format(datetimecol, '%H:%i%s')

2 ways to solve this.

1st from your query, use:

$query = $pdo->prepare("SELECT time(datetimecol) as thetime FROM timer"); 
$query->execute();

you get exact thetime is time

2nd, use full format datetime and convert it to time only in php

$return = 30 Jun 2017 03:33:52
$return = date("H:i:s", strtotime($return)); // 03:33:52

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM