0

My objective is to convert a mySQL database item now in a 24-hour time format to a 12-hour AM/PM format using the PHP that queries and adapts the data for use in a calendar.

Current mySQL item example:

(Name) start_time | (Type) varchar(20) | (Collation) utf8_general_ci |
(Null) No | (Default) None

Current database PHP query/array item:

$timetable->start_time  = $db_timetable['start_time'];

Returns the database content as entered, i.e.: 13:30

Proposed database PHP query/array item:

$timetable->start_time  = DATE_FORMAT($date,
'%r'($db_timetable['start_time']));

Want it to be converted to: 1:30 PM

I’ve tried numerous variations on the proposed query, but with no luck. Any suggestions would be appreciated.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • it's not a full date, so you can't use date functions, you can do a workaround, explode the value using ':' separator, then the value in index 0 you can check if lower than 12 then - from 24 and use pm otherwise use am. – Momen Shaker Jan 01 '23 at 21:40
  • This isn't valid PHP code. It's hard to tell what you're even intending here. Is that the PHP [`date_format()`](https://www.php.net/manual/en/function.date-format.php) function? If so, why is it all upper-case? – tadman Jan 01 '23 at 21:40
  • If the goal is to use the MySQL DATE_FORMAT that needs to be in the query, not where you are accessing the results of the query – ysth Jan 02 '23 at 00:24

1 Answers1

0

'g': 12-hour format of an hour without leading zeros

'i': Minutes with leading zeros

'a': Lowercase Ante meridiem and Post meridiem

This code will output the time in the format h:mm AM/PM, for example: 1:30 PM:

$timetable->start_time = date('g:i A', strtotime($db_timetable['start_time']));
NOMAD
  • 16
  • 3