newbreed65 Posted December 16, 2008 Share Posted December 16, 2008 hi everyone I'm trying to find out if/how I can separate a date that I would have already stored in a database into $month, $day and $year since I'll be using selection boxes for inputting the date and obviously when it comes to editing info on say a movie I would need the right release date showing I've tried looking round for how to do and tried a few things but no luck, can anyone help me or would it actually better if I just stored the date in the database a separate field (month, day, year). Just in case it's useful here is a link to what I made learning how to make the date selection boxes and the code below http://bulletsnoctane.co.uk/date_select.php <?php $d2 = $_POST['d2']; $m2 = $_POST['m2']; $y2 = $_POST['y2']; if($y2 == "") $y2 = date("Y"); ?> <form name=xc action="" method=POST> <?php /* ----------------------- Day Select Box ---------------------------- */ echo "<select name=d2>"; for($value=1; $value<32; $value++) { if($d2 == $value) echo "<option value=$value selected>$value</option>"; else echo "<option value=$value>$value</option>"; } echo "</select>"; /* ----------------------- Month Select Box ---------------------------- The Month Select Box So the Value is the numeric value but but what the user selects is the actual text value of a month like "May" */ /* create array so we can name months */ $monthName = array(1=> "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); echo "<select name=m2>"; for($value = 1; $value <= 12; $value++) { echo "<option value=\""; echo intval($value); echo "\""; if($m2 == $value) { echo " SELECTED"; } echo ">" . $monthName[$value] . "</option>"; } echo "</select>\n"; /* ----------------------- Year Select Box ---------------------------- */ echo "<select name=y2>"; $startYear = date( "Y"); for($value = $startYear - 75; $value <= $startYear+5;$value++) { echo "<option value=\"$value\""; if($y2==$value) { echo " SELECTED"; } echo ">" . $value . "</option>"; } echo "</select>\n"; ?> <input type=submit value="view stats"> </form> <?php $date1 = date("Y-m-d",mktime(0, 0, 0, $m2, $d2, $y2)); include "action.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/ Share on other sites More sharing options...
Mark Baker Posted December 16, 2008 Share Posted December 16, 2008 I'm assuming that you're storing the date in the database as a datestamp rather than a VARCHAR2. What exactly are you trying to do? I'm not certain if you're getting the user to enter a date and then planning on searching for all films due for release on that date, or what? Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716818 Share on other sites More sharing options...
Mchl Posted December 16, 2008 Share Posted December 16, 2008 PHP offers you dozens of datetime functions. strptime might be just one of many you could use (not to mention some more general ones like explode) Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716830 Share on other sites More sharing options...
newbreed65 Posted December 16, 2008 Author Share Posted December 16, 2008 its currently getting stored as a date data type Im making a movie site that as well as news and review im trying to include more detail info about the actual movies and actors like what you would find at imdb.com so im storing information about the movie(name, release date, summary, type etc) and then the users would enter/edit the data, so when it comes to the user editing the data i want the selection boxes to show the correct release date if one has already been entered Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716833 Share on other sites More sharing options...
Mchl Posted December 16, 2008 Share Posted December 16, 2008 For example: $dateSplit = explode("-",date("Y-m-d",strtotime($dateFromDatabase))); echo $dateSplit[0]; echo $dateSplit[1]; echo $dateSplit[2]; Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716840 Share on other sites More sharing options...
premiso Posted December 16, 2008 Share Posted December 16, 2008 First question. Where are you pulling the dates out of the database in that script? As far as storing dates in a database, store them as the actual date and don't do 3 separate fields. It is easier to modify a date as a timestamp than as 3 separate fields. As mentioned above you would use either the explode and strtotime functions to separate your date into the sections you want, day/month/year and then set it much like you have done with the $_POST, except with the data from the database. Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716842 Share on other sites More sharing options...
newbreed65 Posted December 16, 2008 Author Share Posted December 16, 2008 im not pulling any data from the database with that code premiso if i miss led you. code was just me trying to show people how i had the selection box and the date split up. Thank you Mchl for the exmple that is exactly what i was after Quote Link to comment https://forums.phpfreaks.com/topic/137222-solved-separating-a-predefined-date-that-is-stored-in-a-database/#findComment-716847 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.