Tell me how to display time from the column of the table in the format "hours: minutes". I deduce the data from the table with the code shown below, but the time is displayed in the format hours: minutes: seconds. I understand that you need to format something like this: echo date_format($date, 'Ymd H:i:s'); But it did not work out. Help me please.

This is how information from the table is displayed:

 function show_list() { $query = 'SELECT show_id, show_time, show_title, show_author FROM schedule ORDER BY show_time ASC'; $res = mysql_query($query); echo '<h2>Список</h2>'; echo '<table border="1" cellpadding="2" cellspacing="0">'; echo '<tr><th>ID</th><th>Время</th><th>Шоу</th><th>Автор</th><th>Редактировать</th><th>Удалить</th></tr>'; while ($row = mysql_fetch_assoc($res)) { echo '<tr>'; echo '<td>'.$row['show_id'].'</td>'; echo '<td>'.$row['show_time'].'</td>'; echo '<td>'.$row['show_title'].'</td>'; echo '<td>'.$row['show_author'].'</td>'; echo '<td><a href="'.$_SERVER['PHP_SELF'].'?action=editform&id='.$row['show_id'].'">Редактировать</a></td>'; echo '<td><a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$row['show_id'].'">Удалить</a></td>'; echo '</tr>'; } echo '</table>'; echo '<p><a href="'.$_SERVER['PHP_SELF'].'?action=addform">Добавить</a></p>'; } 
  • You can select from the database immediately in the required format via DATE_FORMAT ...... type SELECT show_id, DATE_FORMAT(show_time, '%H:%i') as show_time, show_title, show_author FROM schedule ORDER BY show_time ASC - Alexey Shimansky
  • date ('H: i', $ date) - if the date is stored in the database as a timestamp - Wlodzimierz Sitdikowski
  • I tried. It is impossible to then output to echo. Made a query like this: 'SELECT DATE_FORMAT (show_time, "H: i"), show_id, show_title, show_author FROM schedule ORDER BY show_time ASC' ;. But how then to withdraw, I do not understand. When echo '<td>'. $ Row ['show_time']. '</ Td>'; displays nothing - Artem Novikov
  • In what form is the date in the database? What type of column? - Wlodzimierz Sitdikowski
  • @ ArtemNovikov why should DATE_FORMAT be applied to id ?)) And probably not displayed because when using the function the column will be named differently than you expected .... i.e. the column will be called this way ( DATE_FORMAT(show_time, '%H:%i') ) ... so you should add an alias as show_time - Alexey Shimansky

1 answer 1

 $date = '2016-06-06 12:05:00'; echo date('H:i',strtotime($date)); 

Conclusion

 12:05 
  • Should I prescribe $ date = 'SELECT show_time FROM schedule'? To get the date from the appropriate column? - Artem Novikov
  • in the $ date variable you must put the date value from the database - tCode
  • echo '<td>'. date ('H: i', strtotime ($ row ['show_time'])). '</ td>'; - tCode
  • Thank you very much! Happened! - Artem Novikov