papaface Posted April 10, 2007 Share Posted April 10, 2007 Hello, I'm not very good with working with dates so I need help asap. I have a form input text box that the user puts a date in. In this format 10/04/2007. How can I put that into a database, and then retrieve the results and sort them by date? regards Link to comment https://forums.phpfreaks.com/topic/46467-dates/ Share on other sites More sharing options...
gluck Posted April 10, 2007 Share Posted April 10, 2007 storage in the database could be in many forms like date field type or unix time stamp. Chose any but for retrieval you can used date_format() and provide the desired format. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226052 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 Yeah, how do I do that though Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226056 Share on other sites More sharing options...
boo_lolly Posted April 10, 2007 Share Posted April 10, 2007 look into strtotime(). Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226058 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 I know what functions to use, but I don't know how to format dates so that they can be inserted into a database date field. Then retrieve them, and display/sort them... Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226065 Share on other sites More sharing options...
utexas_pjm Posted April 10, 2007 Share Posted April 10, 2007 You can use timestamps or the format <?php date('Y-m-d h:i:s'); ?> Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226072 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 <?php $date = strtotime($_POST['date']); echo $date; $convert = date('d-m-Y',$date); echo "<br><br>". $convert; ?><form action="test.php" method="post"><input name="date" type="text" id="date" /></form> when i use that, and put in the date 05/04/2007 (april the 5th) it changes it so it comes out 04-05-2007 (4th of april) Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226083 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 I have a site that uses dates - and to insert I use: $date = $db->escape($_POST['date']); $db->query("INSERT INTO table SET date = '$date'"); The to display the date on a page: echo date("l - F j, Y",strtotime($date)); Which displays dates as Tuesday - April 10, 2007 Hope that helps. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226085 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Also - heres a list of the formats for dates - http://us.php.net/manual/en/function.date.php so you can output it however you would like. Just remember, a date field in an a MySQL database is 2007-04-10 - so either have them entered like that, or converted to that before they are inserted to the database. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226097 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 I use this code to put it into the db: $date = strtotime($_POST['date']); $convert = date('Y-m-d',$date); $_insert = mysql_query("insert into `requests` (name,address,postcode,telephone,email,inspectaddress,stage,area,date,status) values ('".$_POST['name']."','".$_POST['custaddress']."','".$_POST['postcode']."','".$_POST['telephone']."','".$_POST['email']."','".$_POST['inspectaddress']."','".$_POST['stage']."','".$_POST['area']."','{$convert}','unassigned')"); It goes in like this: 2007-05-04 I then try retrieve the date and it comes out like this 2007-05-04. How can I change it, so it goes day (05) month (04) and then year? I tried: while (list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned)) { $date = gmdate('d-m-Y',$date); But it doesnt work Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226137 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Is the field it's inserting into a DATE field in the databse? Try: $date= date("d-m-Y",strtotime($date)); Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226158 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 Yes. I will try your code. 2 mins Edit: That kinda works It changes the date to 04-09-2007 But thats the 4th of september. When I need it to be the 9th of april. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226160 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Switch the d-m - so: $date= date("m-d-Y",strtotime($date)); Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226161 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Actually scratch that - 04-09-2007 ... how is that stored in your database? is it stored as 2007-04-09 ? Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226165 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 No. 2007-09-04 Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226170 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Should be stored as Y-m-d . So 2007-04-09. When it's being entered into the form, are you entering as 09-04-2007 - then converting to 2007-04-09. If it's just you using the form, you could skip the conversion and enter it as 2007-04-09. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226173 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Actually looking back - if you enter in the format of 31-4-07 (I'm using 31 so we know its a day not a month) and convert to date(Y-m-d) then when you call it up you can use: $date= date("d-m-Y",strtotime($date)); Try that out. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226179 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 Ive changed it (when its put in the db) from: $convert = date('Y-m-d',$date); to $convert = date('Y-d-m',$date); Its put into the db like this 1970-01-01 but the date is obviously wrong. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226182 Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 $date must be a unix time stamp to be manipulated. It seems as though you ignored what JSHINER wrote completely... Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226186 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 How it was put into the database is fine... change how you call it up: while (list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned)) { $date= date("d-m-Y",strtotime($date)); When you get that date of 1970-01-01 you know theres an error - thats the date MySQL starts counting from. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226188 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 $date must be a unix time stamp to be manipulated. It seems as though you ignored what JSHINER wrote completely... No i didnt. It is converted: $date = strtotime($_POST['date']); $convert = date('Y-m-d',$date); But now it doesnt work. $date = strtotime($_POST['date']); $convert = date('Y-m-d',$date); $_insert = mysql_query("insert into `requests` (name,address,postcode,telephone,email,inspectaddress,stage,area,date,status) values ('".$_POST['name']."','".$_POST['custaddress']."','".$_POST['postcode']."','".$_POST['telephone']."','".$_POST['email']."','".$_POST['inspectaddress']."','".$_POST['stage']."','".$_POST['area']."','{$convert}','unassigned')"); That is my code. It is entered into the input box like this: 31/04/2007 I want it to come out like that aswell. I'm sorry for all this confusion, but I just hate dates. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226192 Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 What is the column for mysql, date/time if so it needs to be put in that exact format for it to be excepted by SQL. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226194 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 It is a date field. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226195 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Ok the way it goes in is fine. Now let's pull it from the database using the follwoing code: while (list($request_id,$name,$address,$postcode,$telephone,$email,$inspectaddress,$stage,$area,$date) = mysql_fetch_array($_getunassigned)) { $date= date("d-m-Y",strtotime($date)); That code will display 2007-04-31 (the way it is stored) as 31-04-2007. date("d-m-Y") is day-month-year. Let me know if that works for you. They key to dates is getting them into the database correctly. Once they're in there correctly, you can manipulate them however you would like. Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226196 Share on other sites More sharing options...
papaface Posted April 10, 2007 Author Share Posted April 10, 2007 Ok the way it goes in is fine. No its not thats the thing. Its putting in 1970-01-01 Link to comment https://forums.phpfreaks.com/topic/46467-dates/#findComment-226197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.