Jump to content

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.