phingoc Posted December 4, 2011 Share Posted December 4, 2011 yeah... in norway, we use dd.mm.yyyy, but mysql date uses yyyy.mm.dd.. Is there any php lines i can use to rearrange the input dates? And also, rearrange the date when i query the mysql for the dates? Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/ Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2011 Share Posted December 4, 2011 Are the submitted dates coming from a user submitted form? How are the users inputting the dates, with <select> fields, or something else? Formatting the dates for display can be easily done with the DATE_FORMAT() function in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/#findComment-1294331 Share on other sites More sharing options...
phingoc Posted December 4, 2011 Author Share Posted December 4, 2011 <input type="text" size="12" maxlength="10" name="dato" value="<?php echo date('d.m.Y'); ?>"> user has the ability to remove the php generated date, and input his own date. Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/#findComment-1294333 Share on other sites More sharing options...
scootstah Posted December 4, 2011 Share Posted December 4, 2011 Don't store dates like that. Either use MySQL DATETIME or PHP time(). Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/#findComment-1294337 Share on other sites More sharing options...
phingoc Posted December 4, 2011 Author Share Posted December 4, 2011 found out a way to sort it out. $endredato = strtotime($dato); $nydato = date('d-m-Y', $endredato); easy, but had to change about 15 php files. Thank you guys for answers. Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/#findComment-1294340 Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2011 Share Posted December 4, 2011 Irrespective of the language used, the best (easiest for the user) way to handle date input by users is to use a jQuery datepicker with <select> fields enclosed in <noscript></noscript> tags for the users who don't allow javascript. The second best way is to use only <select> fields. It wouldn't take much work to change your form and processing code for them, just concatenate the values into the YYYY-MM-DD format for insert into the DB. Whether you decide to implement a jQuery solution or not, the <select> fields are easy to generate dynamically, and help constrain the user to the proper format. I usually try not to just post a ready made solution, but I keep this code stored as a snippet that I use often anyhow. If you want to use it, feel free to do so. <?php // build day field $curday = date('d'); echo "<select name=\"day\">\n"; for( $i = 1; $i < 32; $i++ ) { $sel = $curday == $i ? $sel = 'selected="selected"' : ''; echo "<option value=\"$i\" $sel>$i</option>\n"; } echo "</select>\n\n"; // build month field $month = array( 1 => 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'); // display format is set when value is echoed $curmonth = date('m'); echo "<select name=\"month\">\n"; foreach( $month as $k => $v ) { $sel = $k == $curmonth ? 'selected="selected"' : ''; echo "<option value=\"$k\" $sel>" . ucfirst(substr($v, 0, 3)) . "</option>\n"; // Display format is set in this line using ucfirst and substr . . . } echo "</select>\n\n"; // build year field $curyear = date('Y'); $yr_begin = $curyear - 100; // starts list from 100 years ago $yr_end = $curyear + 0; // ends list at current year echo "<select name=\"year\">\n"; for( $i = $yr_end; $i >= $yr_begin; $i-- ) { $sel = $i == $curyear ? 'selected="selected"' : ''; echo "<option value=\"$i\" $sel>$i</option>\n"; } echo "</select>\n\n"; Quote Link to comment https://forums.phpfreaks.com/topic/252451-rearrange-input-date-from-ddmmyyyy-to-yyyymmdd/#findComment-1294346 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.