runnerjp Posted February 3, 2009 Share Posted February 3, 2009 hey.. i have a date stored in my db under varchar like this 19/12/1987 how can i split it up so that i can use it here <?php echo "Day: <select name='day'>"; foreach ($days as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } echo '</select>'; echo "Month: <select name='month'>"; foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } echo '</select>'; echo "Year: <select name='year'>"; foreach ($years as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } ?> so that in the drop down menu's the stored date is selected? Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/ Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 If you're always gonn have that style of date to split you could just use explode; <?php $your_date = "19/12/1987"; $splitDate = explode("/", $your_date); echo $splitDate[0];//day echo $splitDate[1];//month echo $splitDate[2];//year Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753480 Share on other sites More sharing options...
Mikedean Posted February 3, 2009 Share Posted February 3, 2009 This is how I would have done it (I've only done it for the days, but you should see how to do it for months etc.) $your_date = strtotime( "19/12/1987" ); echo "Day: <select name='day'>"; for( $i = 1; $i < 32; $i++ ) { $i = str_pad( $i, 2, STR_PAD_LEFT, 0 ); echo "<option value='" . $i . "'" . ( $i == date( "d", $your_date ) ? " selected='selected'" : "" ) . ">" . $i . "</option>"; } echo "</select>"; Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753504 Share on other sites More sharing options...
runnerjp Posted February 3, 2009 Author Share Posted February 3, 2009 This is how I would have done it (I've only done it for the days, but you should see how to do it for months etc.) $your_date = strtotime( "19/12/1987" ); echo "Day: <select name='day'>"; for( $i = 1; $i < 32; $i++ ) { $i = str_pad( $i, 2, STR_PAD_LEFT, 0 ); echo "<option value='" . $i . "'" . ( $i == date( "d", $your_date ) ? " selected='selected'" : "" ) . ">" . $i . "</option>"; } echo "</select>"; i tred it on a test page...this exact code and it does not display the selected lol .. Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753514 Share on other sites More sharing options...
runnerjp Posted February 3, 2009 Author Share Posted February 3, 2009 i went with gevans idea as it worked and it was easy...thank guys Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753520 Share on other sites More sharing options...
runnerjp Posted February 3, 2009 Author Share Posted February 3, 2009 sorry i seem to be doing somethign wrong here as it displays the bottom of the menu rather then the selcted? <?php $your_date = $getuserprofile['dob']; $splitDate = explode("-", $your_date); echo $splitDate[0];//day echo $splitDate[1];//month echo $splitDate[2];//year // i have cut the code between out $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'); $weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $days = range (1, 31); $years = range (1910, 2015); //********************************************** echo "Day: <select name='day'>"; foreach ($days as $value) { { echo '<option value="'.$value.'" selected="'.$splitDate[0].'">'.$value.'</option>\n'; } echo '</select>'; echo "Month: <select name='month'>"; foreach ($months as $value) { echo '<option selected="'.$splitDate[1].'" value="'.$value.'">'.$value.'</option>\n'; } echo '</select>'; echo "Year: <select name='year'>"; foreach ($years as $value) { echo '<option selected="'.$splitDate[2].'" value="'.$value.'">'.$value.'</option>\n'; } ?> Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753532 Share on other sites More sharing options...
MatthewJ Posted February 3, 2009 Share Posted February 3, 2009 In order to select a certain value in the select menu, you put selected='selected' on the specific item. It looks like you are setting selected='21'. You need to check during the loop that is outputting the select menu for the desired number to select... if it is that number in the loop, add the selected attribute. $num = 5 foreach($array as $v) { if($v == valueofsplidate) { echo "<option value='$v' selected='selected'>$v</option>"; } else { echo "<option value='$v'>$v</option>"; } Something along those lines Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753552 Share on other sites More sharing options...
runnerjp Posted February 3, 2009 Author Share Posted February 3, 2009 but where does $numn come into it :S Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753562 Share on other sites More sharing options...
MatthewJ Posted February 3, 2009 Share Posted February 3, 2009 It was just an example, I changed and added the "valueofsplitdate" instead of checking the number and forgot to delete $num = 5; ... If you are doing the day field, you need to check for the entered day, etc. For day: foreach($days as $value) { if($value == $splitDate[0]) { echo "<option value='$value' selected='selected'>$value</option>"; } else { echo "<option value='$value'>$value</option>"; } Link to comment https://forums.phpfreaks.com/topic/143608-spliting-up-a-date/#findComment-753573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.