jeff5656 Posted December 23, 2012 Share Posted December 23, 2012 (edited) I have a date field in a form. If a user enters 12/23 instead of 12/23/12 I want to add the current year before putting it into the datatbase. The format in the database is Y-m-d because it's a date field. How do I modify this code to do that? I'm sorry I'm a bit weak with dates, especially if I have to stray from the standard date code! $this_date = date("Y-m-d", strtotime($_POST['this_date '])); $query = "insert into table (this_date) values ('$this_date')"; If a user enters 12/23/12 or 12/23/2012, I do not need to add the current year, and the above code will work, so I need the code to take into account a user entering the year OR not entering it. Edited December 23, 2012 by jeff5656 Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/ Share on other sites More sharing options...
Manixat Posted December 23, 2012 Share Posted December 23, 2012 (edited) You can use date('y') to get the current year and add it to the input if the length is less than 6? Although I recommend you convert your date to a Unix timestamp before saving it in your database, that way it is really easy to manipulate in future Edited December 23, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1400996 Share on other sites More sharing options...
jeff5656 Posted December 23, 2012 Author Share Posted December 23, 2012 (edited) How do a) check if the user has only added month and day and b, add the current year to the variable $this_date if the user has not entered the year? Edited December 23, 2012 by jeff5656 Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1400997 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2012 Share Posted December 23, 2012 You shouldn't allow users to directly type dates. Even if you display the correct format next to the input field, you will get all kinds of incorrect input. You need to use a date picker/three separate drop-down select menus. You should also not store unix timestamps since you must perform an ambiguous, timezone dependent, conversion on them to use for anything other than simple comparisons. Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1400998 Share on other sites More sharing options...
jeff5656 Posted December 23, 2012 Author Share Posted December 23, 2012 But does anyone know how to do this, or is it something that can't be done. I feel that PHP can do anything really, but this is a bit out of my range - that's why I came here because I know someone out there knows! I appreciate the feedback about user input etc, but if there is anyone that knows how to do this I would love to see what the code looks like that would make this work. Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1401002 Share on other sites More sharing options...
Manixat Posted December 23, 2012 Share Posted December 23, 2012 (edited) PFMaBiSmAd Makes a good point there, jQuery offers a really nice and easy to use datepicker feature, you should check it out at http://jqueryui.com/datepicker/ Edited December 23, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1401005 Share on other sites More sharing options...
Barand Posted December 23, 2012 Share Posted December 23, 2012 (edited) $input = '12/23'; echo date('Y-m-d', strtotime($input)); //--> 2012-12-23 $input = '12/23/12'; echo date('Y-m-d', strtotime($input)); //--> 2012-12-23 Safer to use a date picker though Edited December 23, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1401008 Share on other sites More sharing options...
jeff5656 Posted December 23, 2012 Author Share Posted December 23, 2012 Thanks for that code - but how does it know that the current year is 2012 if all they type is 12/23? Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1401010 Share on other sites More sharing options...
Barand Posted December 23, 2012 Share Posted December 23, 2012 Smoke and mirrors. Plus it will default to the current year if none specified Quote Link to comment https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/#findComment-1401011 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.