Jump to content

MYSQL DATE FORMAT IN PHP TABLE


dilby

Recommended Posts

Hi all -

 

I am parsing info from mysql into a php table but the date being returned is in YYYY-MM-DD format and I want it DD-MM-YYYY. How would I do this?

 

Code at the moment is:

 

$link = connect();

function get_user_posts(){

$getPosts = mysql_query('SELECT a.id as id,a.post as question, a.answer as answer, a.created as created, a.updated as updated, b.vclogin as admin
 FROM user_posts as a, admin as b WHERE a.admin_id=b.adminid',connect());
return $getPosts;
}

 

<table class="list">
<tr>
	<th><?php echo getlocal("upost.post") ?></th>
	<th><?php echo getlocal("upost.answer") ?></th>
	<th><?php echo getlocal("upost.admin") ?></th>
	<th><?php echo getlocal("upost.created") ?></th>
	<th><?php echo getlocal("upost.updated") ?></th>
	<th></th>
	<th></th>
</tr>
<?php 
$all_questions = get_user_posts();

while ($question = mysql_fetch_assoc($all_posts)):
?>

<tr>
	<td><?php echo $post['post']?></td>
	<td><?php echo $post['answer']?></td>
	<td><?php echo $post['admin']?></td>
	<td><?php echo $post['created']?></td>
	<td><?php echo $post['updated']?></td>
	<td><a href="javascript:user_post('<?php echo $post['id']?>')">edit</a></td>
	<td><a href="javascript:delete_post('<?php echo $post['id']?>')">delete</a></td>
</tr>

 

THANKS!

 

Link to comment
Share on other sites

mysql's DATE_format is the easiest and best way IMO. You don't need to mess with the date in php, just do this with the select statement. Assuming your datetime field is called 'thedate':

 

SELECT DATE_FORMAT(thedate, '%M %e, %Y, %l:%i%p') as newdate FROM your_table;

or even this if you like...

SELECT *, DATE_FORMAT(thedate, '%M %e, %Y, %l:%i%p') as newdate FROM your_table;

 

It will return a field called 'newdate' with the formatting you want.

 

See this page [dev.mysql.com] at mysql.com/doc for all the formatting options.

source: http://www.webmasterworld.com/forum88/11945.htm

Link to comment
Share on other sites

Wherever you have a date vairable, for example if $post['updated'] is a date there, do this instead:

 

change this:

<?php echo $post['updated']?>

 

to:

 

<?php echo implode('-', array_reverse(explode('-', $post['updated'])))?>

 

The only thing it does is changing dd-mm-yyyy to yyyy-mm-dd

But litebearer's way is better. I'd go with that.

Link to comment
Share on other sites

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.