justin7410 Posted May 1, 2013 Share Posted May 1, 2013 Hey guys, So i have a small yet very simple question more so than an issue. I am trying to call a variable that is a field from a mysql database. i am using an extract() function so any field title just returns as a variable. i have a member page where i then list the member and want to have on the side of their name the year they are born. for ex. <h1> Name ( year ) </h1> So in my data field the year the person is born is given as a full string of text 1984-14-09 Y/dd/m. All i want is the year from this data and to remove the day/month. So i did some research , and i came up with the solution of using an explode(), creating an array, and then simple echoing the index of that data i want. Problem is that the array is then leaving the entire string as one index in the array, basically not doing what i wanted it to do, which was to split the string into parts, allowing me to then echo whatever part i wanted. my code: <h1><a href="#link"><? echo $member_name; ?> (<? $arr = explode(' ',trim($member_birth_date)); print_r($arr); ?>)</a></h1> end result: Jimmy Dean (Array ( [0] => 2006-04-21 ) ) instead of Jimmy Dean (Array( [0] => 2006 [1] => - [2] => 04 [3] => - [4] => 21 ) any suggestions as to how to get the result i am looking for ? would really appreciate any ideas or solutions thanks guys Link to comment https://forums.phpfreaks.com/topic/277515-how-to-grab-the-first-word-from-a-variable-that-is-a-string/ Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 Use the correct date format (type DATE, format yyyy-mm-dd) and you can then use MySql's datetime functions to make life easy eg SELECT YEAR(theDate) from mytable Link to comment https://forums.phpfreaks.com/topic/277515-how-to-grab-the-first-word-from-a-variable-that-is-a-string/#findComment-1427622 Share on other sites More sharing options...
justin7410 Posted May 1, 2013 Author Share Posted May 1, 2013 Use the correct date format (type DATE, format yyyy-mm-dd) and you can then use MySql's datetime functions to make life easy eg SELECT YEAR(theDate) from mytable Yes thank you Barand, this was a helpful tip , and i tried it , but before i close this thread an answered if there is any other tips or solutions not having to use Mysql and strickly php function. Link to comment https://forums.phpfreaks.com/topic/277515-how-to-grab-the-first-word-from-a-variable-that-is-a-string/#findComment-1427636 Share on other sites More sharing options...
Barand Posted May 1, 2013 Share Posted May 1, 2013 One PHP way is list ($year, $day, $month) = explode ('-', $thedate); I mentioned the date format as the one you are using is useless for a database - you can't compare dates in that format so therefore you cannot sort them either. Nor can you use the functionality of MySql with first converting them. Link to comment https://forums.phpfreaks.com/topic/277515-how-to-grab-the-first-word-from-a-variable-that-is-a-string/#findComment-1427641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.