jacko_162 Posted May 9, 2010 Share Posted May 9, 2010 i have a stored date in database as yyyy-mm-dd if i wanted to echo this i would get "2010-05-09" how could i echo the above variable from database as: "May 9, 2010" Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2010 Share Posted May 9, 2010 Use the mysql DATE_FORMAT() function in your query to return a DATE value formatted any way you want - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055465 Share on other sites More sharing options...
jacko_162 Posted May 9, 2010 Author Share Posted May 9, 2010 Use the mysql DATE_FORMAT() function in your query to return a DATE value formatted any way you want - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format i had a read through and came up with the following code; echo date_format($date, 'Y-m-d'); i get a Fatal error: Call to undefined function: date_format() i assume i dont get this in my version of PHP... any work arounds? Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055467 Share on other sites More sharing options...
kenrbnsn Posted May 9, 2010 Share Posted May 9, 2010 There's the DATE_FORMAT function in MySQL that called via the query, then there's the date function in PHP used in conjunction with the strtotime function. You're confusing the two. If you want to use the PHP function, do something like: <?php echo date('F j, Y',strtotime($date)); ?> assuming that the $date variable holds the value from your database. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055474 Share on other sites More sharing options...
Pikachu2000 Posted May 9, 2010 Share Posted May 9, 2010 Because it's a MySQL function, not a PHP function. In the DB query: SELECT DATE_FORMAT(field_name, '%b %e, %Y') AS date FROM tbl_name . . . should do it. Then your array element with the date will be along the lines of $array['date']. Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055475 Share on other sites More sharing options...
jacko_162 Posted May 9, 2010 Author Share Posted May 9, 2010 There's the DATE_FORMAT function in MySQL that called via the query, then there's the date function in PHP used in conjunction with the strtotime function. You're confusing the two. If you want to use the PHP function, do something like: <?php echo date('F j, Y',strtotime($date)); ?> assuming that the $date variable holds the value from your database. Ken worked a treat, ty Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055477 Share on other sites More sharing options...
jacko_162 Posted May 9, 2010 Author Share Posted May 9, 2010 Hopefully my last question today in relation to this whole time/date thing. i have the following php code; <?php function imp($char,$tag){ foreach($char as $key=>$value){ $char[$key] = $value; } $char = implode($tag,$char); return $char; } ?> <?php $sql = "SELECT * FROM `tests` WHERE date <= '$todaydate' AND date >= '$oldDate' AND member_id='1' ORDER BY ID ASC LIMIT 0,10"; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ $test1[] = $row[test1]; $test2[] = $row[test2]; $test3[] = $row[test3]; $test4[] = $row[test4]; $test5[] = $row[test5]; $test6[] = $row[test6]; $test7[] = $row[test7]; $test8[] = $row[test8]; $test9[] = $row[test9]; $test10[] = $row[test10]; $test11[] = $row[test11]; $test12[] = $row[test12]; $test13[] = $row[test13]; $test14[] = $row[test14]; $date[] = $row[date]; }} ?> <?php echo imp($test14,",")?> the $test1 variable is then added like so: 1,2,3,4,5,6,7,8,9,10 if i do the same to date i get; 2010-05-09,2010-05-09,2010-05-09 etc.... how can i format the date with the above ideas to show like this; 06.05.10 (basically dd.mm.yy) appreciate the help and the learning curves Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055567 Share on other sites More sharing options...
Joshua4550 Posted May 9, 2010 Share Posted May 9, 2010 If I understand what you mean right, it's similar to before - the format now would simple be "d, m, y" so: date('d.m.y', strtotime($date)); For future reference on date/time formatiing, please referr to: http://php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055574 Share on other sites More sharing options...
jacko_162 Posted May 10, 2010 Author Share Posted May 10, 2010 thank you for reply, yes that bit i get, when i echo it out i get "09.05.10" which is perfect. but the above code needs to list out the entire row of results for example i want it to echo the following; 11.04.10,06.05.10,07.05.10 etc.. so seperating each result with a "," the coding in my above post ouputs the date like this; "2010-04-11,2010-05-06,2010-05-07,2010-05-0" if i use; <?=imp($date,",")?> how and where can i modify the above coding to ouput the date like this? Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055583 Share on other sites More sharing options...
Joshua4550 Posted May 10, 2010 Share Posted May 10, 2010 Not a major at PHP, but couldn't you just use a for statement for format each one before you use the imp() function? Like so: for ($i = 0; $i <= count($date); $i++) { $date[$i] = date('d.m.y', strtotime($date[$i])); } Pretty sure this would work, and then you'd use your: imp($date,",") Only thing is that i'm not aware of any more-efficient methods of doing this, so if someone posts a better one then i'll learn something too! But yeah, that loop should do what you're seeking? Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055586 Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 Replace <?php $date[] = $row[date]; ?> with <?php $date[] = date('d.m.y',strtotime($row[date])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055587 Share on other sites More sharing options...
Joshua4550 Posted May 10, 2010 Share Posted May 10, 2010 ^ That's a more efficient way, then. I also thank you ken. And i'm sorry, I just wasn't sure whether you could format arrays I know a lot more Java, so I tend to go about doing it how I would in Java, incase I mess anything up - and in Java you wouldn't be able to do that! Please post if Ken's code works! Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055588 Share on other sites More sharing options...
jacko_162 Posted May 10, 2010 Author Share Posted May 10, 2010 you the man ken you dont know how many combinations i tried to get that. problem solved. and thx joshua Quote Link to comment https://forums.phpfreaks.com/topic/201174-display-stored-date-differently/#findComment-1055594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.