different date format in different browsers
Moderator: General Moderators
different date format in different browsers
Hi, Newbie here.
I was working on creating a validation rule for a date field in a form and noticed that the date format in Firefox is yyyy/mm/dd while Chrome is dd/mm/yyyy and storing date into mysql is yyyy-mm-dd. I am wondering: does it matter how you enter a date in a field or in what format you send it to mysql for storage? Chrome comes with this neath little popup calender while Firefox is just a plain old text box. Can anyone make some sense out of all that for me please?
I was working on creating a validation rule for a date field in a form and noticed that the date format in Firefox is yyyy/mm/dd while Chrome is dd/mm/yyyy and storing date into mysql is yyyy-mm-dd. I am wondering: does it matter how you enter a date in a field or in what format you send it to mysql for storage? Chrome comes with this neath little popup calender while Firefox is just a plain old text box. Can anyone make some sense out of all that for me please?
Re: different date format in different browsers
Are you using an input with type=date? They may show the date differently but they'll submit it with the form data in a proper format. Regardless, you should parse (like with strtotime()) the value and reformat it to Y-m-d.
Re: different date format in different browsers
here is another little funny thing.
I inserted a jquery date picker in the form and it works great inside chrome but I get 0000-00-00 back when I use it in firefox?
I inserted a jquery date picker in the form and it works great inside chrome but I get 0000-00-00 back when I use it in firefox?
Re: different date format in different browsers
give me an example with thisrequinix wrote:Are you using an input with type=date? They may show the date differently but they'll submit it with the form data in a proper format. Regardless, you should parse (like with strtotime()) the value and reformat it to Y-m-d.
Code: Select all
if (isset($_POST['dob']) && !empty ($_POST['dob'])){
if (strtotime($_POST['dob']) != YYYY-MM-DD) {
$errors[] = 'please insert date with the YYYY-MM-DD FORMAT!';
}
}
Re: different date format in different browsers
1. Use strtotime($_POST['dob']) to convert the string date to a Unix timestamp.
2. If the function returned false then it wasn't a valid time and there was something wrong with the form.
3. Otherwise turn the Unix timestamp into a date string with date(). There are other options but that's the most straightforward.
4. Use that date string in your SQL.
2. If the function returned false then it wasn't a valid time and there was something wrong with the form.
3. Otherwise turn the Unix timestamp into a date string with date(). There are other options but that's the most straightforward.
4. Use that date string in your SQL.
Re: different date format in different browsers
ok this is input:
This is the validation for it:
This works fine but the problem is is someone writes "1967-32-01" it still goes through and brings the date to "0000-00-00" in the database.
there are like millions of tutorial online showing you how to validate a date but every single one I look over so far all have flaws. none really account to really YYYY-MM-DD
Code: Select all
<input type="date ('yyyy-mm-dd')" name="dob" onKeyPress="return disableEnterKey(event)" value="<?php if (isset($_POST['dob']) ){echo htmlentities(strip_tags($_POST['dob']));} else { echo $user['dob']; }?>">Code: Select all
if (isset($_POST['dob']) && !empty ($_POST['dob'])){
if (($_POST['dob'] === false)) {
$errors[] = 'Letters are not allowed in this field!';
}
}there are like millions of tutorial online showing you how to validate a date but every single one I look over so far all have flaws. none really account to really YYYY-MM-DD
Re: different date format in different browsers
So where is the code that reformats the value from the form?
Re: different date format in different browsers
I don't want a reformat code. I simply going to output an error message telling the user to enter the date as follow yyyy-mm-dd
the only thing I need is a function that will check to see if the input is in fact yyyy-mm-dd
the only thing I need is a function that will check to see if the input is in fact yyyy-mm-dd
Re: different date format in different browsers
Then make the field a regular text box (type=text) and
1. Match the value against the regular expression /^\d\d\d\d-\d\d-\d\d$/ using preg_match() to check that the value is formatted correctly.
2. explode() on hyphens into the year, month, and date.
3. Use checkdate() to validate the date.
1. Match the value against the regular expression /^\d\d\d\d-\d\d-\d\d$/ using preg_match() to check that the value is formatted correctly.
2. explode() on hyphens into the year, month, and date.
3. Use checkdate() to validate the date.
Re: different date format in different browsers
will this verify that someone didn't enter lets say 2000-14-02 or 2000-12-32? I read that checkdate() only start to work from year 2000. what if someone wrote 1998 as the year? Damn! why are date so complicated. the world revolves around money and time and the industry still hasn't come up with a datepicker that works with all browser hahahaha Ok nevermind, I been at this for two days now I need a break from validating dates lol. If I was to do this in MS access I would have that done in 2 second with an input mask.
Re: different date format in different browsers
I couldn't sleep so I figure I tr something else
if (isset($_POST['dob']) && !empty ($_POST['dob'])){
if (checkdate($_POST['dob'] === false)) {
$errors[] = 'This is not a valid date!';
}
}
Why am I not getting any error when I insert a date that is not in the yyyy-mm-dd with this validation script? It simple goes into the database as 0000-00-00 when I enter 07-29-2000 but enters it correctly if I enter 1999-02-12
if (isset($_POST['dob']) && !empty ($_POST['dob'])){
if (checkdate($_POST['dob'] === false)) {
$errors[] = 'This is not a valid date!';
}
}
Why am I not getting any error when I insert a date that is not in the yyyy-mm-dd with this validation script? It simple goes into the database as 0000-00-00 when I enter 07-29-2000 but enters it correctly if I enter 1999-02-12
Re: different date format in different browsers
here is an observation... why do so many people want to record a date in mm-dd-yyyy format when mysql only store yyyy-mm-dd?
Re: different date format in different browsers
As requinix already suggested, strtotime will validate the date.
You can then format the date however you like.
Code: Select all
php > var_dump(strtotime('2014-01-10'));
int(1389330000)
php > var_dump(strtotime('2014-14-10'));
bool(false)
php > var_dump(strtotime('2014-12-32'));
bool(false)
php > Because it's what they're used to writing.why do so many people want to record a date in mm-dd-yyyy format
Re: different date format in different browsers
I;ve tried that but it doesnt' work for me. maybe my syntax is incorrect?
What happen is the date get sent to mysql like this: 0000-00-00 when I try entering a date in the wrong format.
Code: Select all
if (isset($_POST['dob']) && !empty ($_POST['dob'])){
if (strtotime($_POST['dob']) != YYYY-MM-DD) {
$errors[] = 'please insert date with the YYYY-MM-DD FORMAT!';
}
}
Re: different date format in different browsers
There are a few things wrong here. First, you're trying to compare to an undefined constant (YYYY-MM-DD; it needs to be enclosed in quotes for it to be a string). Second, strtotime() returns a UNIX timestamp, or false on failure. The comparison would fail even if you had used a string. Perhaps something like this?
Code: Select all
if (isset($_POST['dob']) && !empty($_POST['dob'])) {
$dob_timestamp = strtotime($_POST['dob']);
if ($dob_timestamp === false) {
$errors[] = "Not a valid date.";
} else {
$dob = date('Y-m-d', $dob_timestamp);
}
}