dilby Posted January 12, 2011 Share Posted January 12, 2011 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 https://forums.phpfreaks.com/topic/224164-mysql-date-format-in-php-table/ Share on other sites More sharing options...
asmith Posted January 12, 2011 Share Posted January 12, 2011 $dateString is your date variable, do this with it: $dateString = implode('-', array_reverse(explode('-', $dateString))); Link to comment https://forums.phpfreaks.com/topic/224164-mysql-date-format-in-php-table/#findComment-1158296 Share on other sites More sharing options...
dilby Posted January 12, 2011 Author Share Posted January 12, 2011 Thanks. Where would I put it though and then how would I define what date format i wanted? (Sorry, should have mentioned im a newbie) Link to comment https://forums.phpfreaks.com/topic/224164-mysql-date-format-in-php-table/#findComment-1158298 Share on other sites More sharing options...
litebearer Posted January 12, 2011 Share Posted January 12, 2011 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 https://forums.phpfreaks.com/topic/224164-mysql-date-format-in-php-table/#findComment-1158299 Share on other sites More sharing options...
asmith Posted January 12, 2011 Share Posted January 12, 2011 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 https://forums.phpfreaks.com/topic/224164-mysql-date-format-in-php-table/#findComment-1158300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.