different date format in different browsers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

different date format in different browsers

Post by jaad »

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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: different date format in different browsers

Post by requinix »

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.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

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?
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

requinix 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.
give me an example with this

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!';
					}	
				}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: different date format in different browsers

Post by requinix »

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.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

ok this is input:

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']; }?>">
This is the validation for it:

Code: Select all


if (isset($_POST['dob']) && !empty ($_POST['dob'])){
	if (($_POST['dob'] === false)) {
	$errors[] = 'Letters are not allowed in this field!';
	}	
}
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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: different date format in different browsers

Post by requinix »

So where is the code that reformats the value from the form?
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: different date format in different browsers

Post by requinix »

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.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

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.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

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
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: different date format in different browsers

Post by Celauran »

As requinix already suggested, strtotime will validate the date.

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 > 
You can then format the date however you like.
why do so many people want to record a date in mm-dd-yyyy format
Because it's what they're used to writing.
User avatar
jaad
Forum Commoner
Posts: 95
Joined: Fri Jan 03, 2014 5:30 am
Location: Vancouver Canada

Re: different date format in different browsers

Post by jaad »

I;ve tried that but it doesnt' work for me. maybe my syntax is incorrect?

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!';
         }      
 }

What happen is the date get sent to mysql like this: 0000-00-00 when I try entering a date in the wrong format.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: different date format in different browsers

Post by Celauran »

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);
	}
}
Post Reply