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

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.