Jump to content

Syntax guidance to use mysql datetime for html table with php


ThisisForReal

Recommended Posts

I'm currently storing some date and time information in a MySQL column that has a date time format: "2010-06-04 17:35:00". I am in an html table using php's foreach { to cycle through my query results and create <td> tags to separate the date and time into separate columns within the table.

 

e.g.

echo '<td>' . $date . '</td><td>' . $time '</td>';

 

I would love to display the data to the user in a more useful format ("June 4", "5:35 pm").  I know that to do that I'd be using PHP's 'M' and 'j', for one column and 'g' 'i' and 'a' for the other. 

 

I am having a total brainfart on transforming the data for display (that, and I'm new to php).  You don't need to point it out, but as you can imagine I'm getting the server time returned to me, instead of what's in my DB table:

foreach($meals as $display) {
    $date = $meals['mealDateTime'];
$date = date('M j');
$time = $meals['mealDateTime'];
$time = date('g:i a');
echo '<tr><td>';
echo $date;
echo '</td><td colspan="4">';
echo $time;
echo '</td></tr>';
}

 

I'm just asking what the proper syntax is to get the datetime value returned in my query and use the php date functions to display it correctly.  Thanks so much!

First convert the data to a unix timestamp using strtotime(), then use the date() function to format it:

 

$datetime = strtotime($meals['mealDateTime']);
$date = date('M j', $datetime);
$time = date('g:i a', $datetime);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.