hostfreak Posted July 26, 2006 Share Posted July 26, 2006 I have a date field that I insert dates into in my mysql database. The default is 0000-00-00, when I insert the date I insert it as variablename with the values as variablenameY-variablenamem-variablenamed. That's all fine, but when I got to edit it as it is y-m-d as is expected. However I want to be able to edit it from a drop down (which is how I add that dates, from a dropdown). So what I need to figure out is how to extract the values from the field after they are inserted. Hope that makes sense. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/ Share on other sites More sharing options...
Caesar Posted July 26, 2006 Share Posted July 26, 2006 Can you please show some sample code to see if we can maybe point you in the direction? Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64376 Share on other sites More sharing options...
Caesar Posted July 26, 2006 Share Posted July 26, 2006 Also...I suggest you store dates in your db as timestamps...and then do any formatiing in the actual code for display purposes. Makes life easier. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64377 Share on other sites More sharing options...
ryanlwh Posted July 26, 2006 Share Posted July 26, 2006 you can use the php function strtotime() to convert a date into a timestamp. you can also format the date within a mysql query using DATE_FORMAT(). Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64380 Share on other sites More sharing options...
hostfreak Posted July 27, 2006 Author Share Posted July 27, 2006 Alright here is an example of how I would add it:[code]$connection = mysql_connect("$server","$user","$password"); mysql_select_db ($database);$dateY = $_POST['dateY'];$datem = $_POST['datem'];$dated = $_POST['dated'];$query = "INSERT INTO users(date) VALUES('$dateY-$datem-$dated');"[/code]Then I would make a form that has three dropdown fields. One being for the dateY (year), second being for datem (month), and the third being for dated (day). Then it would insert it into the mysql date field as year-month-day (0000-00-00). So when I go to edit it, I have to edit it in a input field as: 0000-00-00 . I would like to be able to edit it from a dropdown, but I need some way to extract the year-month-day values from the date. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64425 Share on other sites More sharing options...
hostfreak Posted July 27, 2006 Author Share Posted July 27, 2006 I thought about converting it, but that still doesn't allow me to edit it from a dropdown. Having three different ones: Year, Month, Date. I could just make three different fields in the database, but I've already got a lot added like this so was looking for some ideas before I do that. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64426 Share on other sites More sharing options...
hostfreak Posted July 27, 2006 Author Share Posted July 27, 2006 bump, thanks in advance for any new answers. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64535 Share on other sites More sharing options...
448191 Posted July 27, 2006 Share Posted July 27, 2006 It's a fairly simple task, that has little to with dates, but more with strings:[code]<?php$res = mysql_query('SELECT date FROM users WHERE user='.$usr) or die ('Query Failed: '.mysql_error())$row = mysql_fetch_assoc($res);$arr = explode($row['date'],'-');$y = $arr[0]; $m = $arr[1]; $y=$arr[2];?>[/code]and on from there. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64541 Share on other sites More sharing options...
hostfreak Posted July 27, 2006 Author Share Posted July 27, 2006 Hmm, doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64563 Share on other sites More sharing options...
hostfreak Posted July 27, 2006 Author Share Posted July 27, 2006 After looking at the php manual I figured out why. Just needed a simple change:[code]$array = explode('-', $row['date']);$Y = $array[0]; $m = $array[1]; $d = $array[2];[/code]Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64565 Share on other sites More sharing options...
448191 Posted July 27, 2006 Share Posted July 27, 2006 Oops, sorry about that.. You tend to get lazy about remembering the order of arguments when you always have dreamweaver make it soooo easy for you... :P Quote Link to comment https://forums.phpfreaks.com/topic/15748-working-with-dates/#findComment-64849 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.