frimax Posted September 6, 2007 Share Posted September 6, 2007 Hi all, I have a little problem to solve... I use the following script to get the latest values from the 'history' table of my database. My func works fine but i don't know how to Substr the value of the 'date' values ... I need to integrate the following func to get the clean output of my date value when it lists the array data: $date = substr ($date,0,9); Can anyone help me please? Thanks in advance... $query = $DbLink->query("SELECT * FROM history ORDER BY date DESC"); $i = 0; while ($historyx = mysql_fetch_array($query)) { if ($i == 21) { break; } for ($i=0; $i< 2; $i++) { $output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'.$historyx['date'].'</li>'; $i++; } } Frimax Quote Link to comment https://forums.phpfreaks.com/topic/68167-help-please-substr-a-variable-inside-an-array/ Share on other sites More sharing options...
recklessgeneral Posted September 6, 2007 Share Posted September 6, 2007 Hi, The date returned from the database is a string, so it is possible to pass the $historyx['date'] to substr to extract the value, even as part of the $output_value assignment: $output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'. substr($historyx['date'], 0, 9) .'</li>'; However, it would be more readable to use the date() function to format the output into however you want it displayed. You can do something like $history_date = date ("j M, Y", strtotime ($historyx['date'])); which would set $history_date to "3 Feb, 2007" for example. Read the date docs for a whole bunch of formatting options: http://www.php.net/manual/en/function.date.php Also, I'm not fully understanding what you are wanting to achieve with the nested for loop and the "if ($i == 21)" conditional break. Does $i ever get to 21 as it will keep getting reset in the for loop? Unless I'm missing something I'd do away with the for loop all together: $query = $DbLink->query("SELECT * FROM history ORDER BY date DESC"); while ($historyx = mysql_fetch_array($query)) { $output_value .= '<li>'.$historyx['id'].'--'.'$'.$historyx['login'].'--'.$historyx['date'].'</li>'; } Cheers, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/68167-help-please-substr-a-variable-inside-an-array/#findComment-342760 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.