When asking for a date in a HTML form, in the past I was using different solutions:
Using 3 different fields for day, month, year
Using a text field, but adding to the label something like "Start date (example: 31-12-2013)"
Using a reverse date, e.g. 2013-12-31
Now I would like to use HTML5, and just have a input type='date'. This should also help users with mobiles.
For some browsers this may offer a date picker, for some just a text fields. But we all know there are different date formats, and sometimes the user's machine is not configured as he/she would expect. For instance, if I enter a date of 3 January 2014 as 03-01-2014 this can be interpreted as 1 March 2014 in other cultures.
What is the best I can do to help users to prevent mistakes? Is there a way to write an example, without knowing in advance what are his/her browser settings? And how do I know that PHP will interpret the date correctly?
Use ISO 8601 (YYYY-MM-DD) + HTML5's pattern and placeholder attributes
An alternative to falling back to javascript datepickers, might be using a validation (regex) pattern as fallback for browsers that don't support the date type:
<input type="date" placeholder="YYYY-MM-DD" name="date" value="" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" required="required">
While support for the date input type is still very limited, the pattern attribute's support is already more widespread. Of course you'll still need to check all submissions server side, but this should give a nice non-javascript solution for client side validation. You can use the placeholder attribute to show the right format, but since it's not supported by older browsers (IE9 and lower) it might be better to display the example outside of the form field.
html5pattern.com has a nice collection of ready-to-use input patterns, including date patterns but other ones as well. The benefit is that you can use the same pattern for your server side validation as well.
Y-m-d is likely the best method, If you use jQuery UI's date picker you can specify the format.
http://jqueryui.com/datepicker/
you can also show an alternative format.
According to dev.w3.org it will supply you with "A valid full-date as defined in [RFC 3339]".
However there is no way to know what the user meant if they enter 1/2/2014, although 2014-02-01 is unambiguous). As others have mentioned, go with a date picker if possible.
The HTML5 date input specification specifies a full-date format equal to: yyyy-mm-dd. You will receive date in this format always.
Follow this link for more information
Related
if have the following problem. in my html5 datefield, i have the input order dd.mm.yyyy (for example todays date: 27.04.2017).
<input type="date" />
thats correct in my country and timezone. the posted value is in reversed order to my input. its yyyy-mm-dd (for example todays date: 2017-04-27).
is there any way to change the timeformat if the the value is posted?
ive found several solutions but only for the input and not the posted values.
I'm not really sure what you mean but here is something you can try
$time = strtotime($_POST['dateInput']);
if ($time != false) {
$mydate = date('d-m-Y', $time));
}
is there any way to change the timeformat if the the value is posted?
The field submits using a standard format. This is consistent across browsers that support the date input type and is required by the standard.
The user interface, on the other hand, is not consistent across browsers.
You can reliably expect a date field to always submit in the form YYYY-MM-DD so you can write a parser for it in your server side code (and then format it however you like).
Solve this problem on the server.
(You /could/ use JavaScript to parse the date, format it, and write it to a hidden input every time the date input changes … but doing it server side is simpler).
I am giving a input type time in a form tag to get the particular value in php.
<input class="form-control" id="timeslot" type="time" name="timeslot" placeholder="Time" required>
like the above.
I want to get the time which is inserted in the input box with am/pm. Currently when i am inserting the value 12:30 pm in the input box, it is showing as 12:30 only. . Can anyone say how to get the am/pm from input type time in php ?
Please read the following carefully:
By HTML5 drafts, input type=time creates a control for time of the day input, expected to be implemented using “the user’s preferred presentation”. But this really means using a widget where time presentation follows the rules of the browser’s locale. So independently of the language of the surrounding content, the presentation varies by the language of the browser, the language of the underlying operating system, or the system-wide locale settings (depending on browser). For example, using a Finnish-language version of Chrome, I see the widget as using the standard 24-hour clock. Your mileage will vary.
Thus, input type=time are based on an idea of localization that takes it all out of the hands of the page author. This is intentional; the problem has been raised in HTML5 discussions several times, with the same outcome: no change. (Except possibly added clarifications to the text, making this behavior described as intended.)
Note that pattern and placeholder attributes are not allowed in input type=time. And placeholder="hrs:mins", if it were implemented, would be potentially misleading. It’s quite possible that the user has to type 12.30 (with a period) and not 12:30, when the browser locale uses “.” as a separator in times.
My conclusion is that you should use input type=text, with pattern attribute and with some JavaScript that checks the input for correctness on browsers that do not support the pattern attribute natively.
Thanks to user: Jukka K. Korpela
Source: html5 time inputs shows 12 hours
Goal: Convert any local date to the according ISO date
My Approach: http://codepad.viper-7.com/XEmnst
strftime("%Y-%m-%d",strtotime($date))";
Upside: Converts a lot of formats really well
Downside / Problem: Converts strings and numbers that are obviously not a date. E.g.
strftime("%Y-%m-%d",strtotime("A")) => 2012-10-29
strftime("%Y-%m-%d",strtotime("1")) => 1970-01-01
Questions:
Is there a better way to identify and convert dates to ISO dates?
Do you know of any library / regex that is capable of do so in php?
PHP's strtotime() function already does a best-effort attempt at taking an arbitrary string and working out what date format it is.
I dislike this function for a number of reasons, but it does do a reasonable job of working things out, given a string of unknown date format as input.
However, even strtotime()'s best efforts can never be enough, because arbitrary date formats are ambiguous.
There is no way to tell whether 05-06-07 is meant to be the 5th of June 2007 or the 6th of May 2007. Or even the 7th June 2005 (yes, some people do write dates like that).
Simple plain truth: It's impossible.
If you want your dates to be reliable in any meaningfuly way, you must abandon the idea that you'll be able to accept arbitrary input formats.
[EDIT]
You say in the comments that the input is coming from a variety of Excel and CSV files.
The only hope you have is if each of those files is consistent in itself. If you know that a file from a given source will have a given input format, you can write a custom wrapper for each file type that you import, and process it for that format. This is a solution I've used myself in the past, and it does work as long as you can predict the format for the file you're processing.
However, if individual files contain unpredictable or ambiguous dates, then you are out of luck: You have an impossible task. The only way you'll avoid having bad data is to kick back to the suppliers of the files and ask them to fix their data.
I think the problems will really arise when faced with dates such as 5-6-2012 when it is unclear whether you are dealing with 5th June, or 6th May and you could be taking input from European countries where DD MM YYYY is the norm.
If you are analyzing just one input field, then you might have a chance of detecting the delimeters and splitting the string up looking for what might look like a real date.
In this case the PHP function checkdate might come in handy as a last ditch double check.
Be aware also that Mysql (if this is where the data is heading) is also quite lenient about what it will put into a DATE field, the delimeters, the absence of leading zeros etc. But still, you have to get the Y M D order correct for it to have a chance.
I suppose the ultimate answer is to disallow free-text input for dates, but give them pickers - but of course you may not be in a position to influence the incoming date ...
I've got a website where users can potentially type in their own dates before they're sent to the server. So I obviously need to parse what they give me and get it into a standard format before actually using it. So I used PHP's strtotime() function, which is pretty forgiving about what it will accept as input.
But I'm also using date.js on the site and its parse() function is pretty good, too. Should I use that on the user input before sending it to the server? Which one is better?
I'll keep strtotime() on the back end for safety, but if date.js is better I'll add that to the client.
(To clarify, I'm expecting mostly American date formats. But should that change, anything that eases that transition is preferred.)
As long as you feel strtotime() is meeting your needs, there isn't a great reason to change it on the client side. However, strtotime() makes a couple of assumptions which you need to stay on top of:
From the strtotime() documentation:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
If you are allowing your client to send the date in any format they choose, the above could be a source of confusion. I just tested the dates 5/1/12 and 5-1-12 in date.js, and both were parsed as May 1st, 2012. PHP would interpret the two as May 1st 2012 and Jan 12th 2005(!!) respectively.
echo date("Y-M-n", strtotime("5/1/12"));
// 2012-May-5
echo date("Y-M-n", strtotime("5-1-12"));
// 2005-Jan-1 (whoops!)
However, pre-formatting the date has the obvious benefit of some insurance that the entered date is valid. Keeping strtotime() on the backend also ensures that you don't need a JavaScript-enabled client to send requests. Your PHP could still be called as a web service, etc, without the client needing to be a web browser.
Both.
You absolutely do need to validate the date in PHP, because one or more of your users may have disabled Javascript.
Javascript validation is nice because you can have a date entered in an input control, validate it in the onblur handler, and write the validated date back to the control. So after the user types the date of birth 2/5/01 and leaves the control, the date changes to 2 May 2001 and not only does the user knows that the date is interpreted correctly, but strtotime() also gets an unambiguous value.
You can also use much better validation feedback, from a UX point of view. Javascript can do the following (which is probably well known but it took a bit of searching for me to find it). Put an element in the page, like
<div id="DobReply"></div>;
Then the function which validates the Date of Birth can do
document.getElementById("DobReply").innerHTML = "Current age is "+age;
where age is a variable calculated from the entered date of birth, and today's date. As soon as the user leaves the control, the calculated age pops up, and if the user entered the current year (it happens) then she might spot the error immediately.
So I wouldn't get rid of datejs validation either. But all of this is lost if Javascript is disabled, you can get invalid dates, dates in weird formats, or anything. So if you can only have one, keep the strtotime().
In my project I am importing users from a csv file. In that some columns have date values
Eg:- date of birth, project start date, project dead line date etc. I know the date column headers but the user can enter date format in different ways. How can I validate the value get from csv is a valid date or not?
While not always accurate, you can try strtotime(). It's not a perfect solution but it's worth trying. Just read the notes because it can have varying behavior depending on what your date formats look like.
The php function strtotime can take many different date formats and returns a timestamp. If your can make thoughtful assertions on the valid timestamp values, validation can be done with those assertions.
But it could be prone to side effects,like day and month mismatch, so it would be a poor validation. I suggest that you enforce a common date pattern on the input file.
I use Zend_Date, as the constructor will validate the date for me, but of course if you don't expect a precise format you'll get in the kind problems that #gordon points out with 11/10/09 type of date. This will generate you a valid date, but for a default locale/format which can be of course wrong.