vicodin Posted July 8, 2008 Share Posted July 8, 2008 Ok so what i need to do is insert a date into mysql and then when i retreive it, i need mysql to be able to sort it in order of newest to oldest. What my real question is how do i do that when the user will be inputing that date. For example the user has like an event on 09/30/08 he would enter in one input box the event and then the next the date its on. How do i take 09/30/08 and insert it to mysql and be able to sort that? Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/ Share on other sites More sharing options...
ratcateme Posted July 8, 2008 Share Posted July 8, 2008 there are mysql date functions but i find using Unix time stamps to be a lot easier you can do this $time = strtotime ($date); then you will have a Unix time stamp you can sort like an ordinary number and the display it using date(); Scott. Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/#findComment-584206 Share on other sites More sharing options...
vbnullchar Posted July 8, 2008 Share Posted July 8, 2008 just to give you an idea <?php $d = strtotime($_POST['user_date']); $sql = "insert into table(date) values('d') "; ?> <input type='text' name='user_date' /> Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/#findComment-584209 Share on other sites More sharing options...
cooldude832 Posted July 8, 2008 Share Posted July 8, 2008 Dates need to be verified before inserted best way that I've come across doing dates is a 3 input box system Month: <input type="text" name="month" value="" /> Day: <input type="text" name="day" value="" /> Year (4 digit): <input type="text" name="year" /> Assuming you processing with post simple verification can be done by saying <?php $date = date("U", mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'])); if(date("n-j-Y",strtotime($date)) == $_POST['month']."-".$_POST['day']."-".$_POST['year'])){ #its a valid date feel free to use the variable $date as the date or datetime field } else{ invalid date or leading zero issues } ?> only issue on this is that leading zeroes are a slight problem this version comparison assume no leading zeros on user input. Basically it attempts to verify the text input matches a valid date returned from mysql fairly good assumption if it does than the date entered is a valid legal date. Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/#findComment-584210 Share on other sites More sharing options...
bluejay002 Posted July 8, 2008 Share Posted July 8, 2008 Well, I usually use JS, sort of calendar, for date selection... I store the date then directly to MySQL (after sanitation) without changing to a timestamp. Sorting can be done with SQL without the need for timestamp... I didnnt have any problems with that so far though. I usually use the format: YYYY-mm-dd, say, 2008-09-30. I never had trouble since then. Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/#findComment-584212 Share on other sites More sharing options...
vicodin Posted July 10, 2008 Author Share Posted July 10, 2008 perfect... Thanks for all info... got it working! Link to comment https://forums.phpfreaks.com/topic/113678-insert-date-into-mysql/#findComment-586569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.