Bug Report #3088

Validate::date() doesn't handle dates correctly

Added by Thomas Lété about 4 years ago. Updated about 3 years ago.

Status:ClosedStart date:07/16/2010
Priority:NormalDue date:
Assignee:Woody Gilk% Done:

100%

Category:Core
Target version:v3.0.12
Resolution:wontfix Points:

Description

Hello !

Validate::date() uses strtotime which doesn't treat correctly dates from other format than US.

Like, in France, the common date format is d/m/Y instead of Y-m-d in USA.

Should it be possible to change this behavior ?

Thanks,

Have a good day ;)

History

#1 Updated by Sam de Freyssinet about 4 years ago

This is a PHP function and should be treated as such. As with all other uses of strtotime(), the onus is on the developer to ensure that supply dates in a comprehensible format. I recommend using 'd M Y' format, with strtotime() will also be able to parse correctly regardless of the locale.

#2 Updated by Woody Gilk about 4 years ago

  • Status changed from New to Feedback

What would you recommend instead of strtotime? I don't know of a similar function that provides the same flexibility.

#3 Updated by Jeremy Bush about 4 years ago

The common format in the USA is actually m-d-Y.

#4 Updated by Thomas Lété about 4 years ago

One possible solution would be providing a second argument which could be used in addition of strptime ([[http://be2.php.net/manual/en/function.strptime.php]])

#5 Updated by Human Internals about 4 years ago

Do notice that strtotime returns valid dates to other strings such as 'Next Sunday' , '+30 days' etc.
You wouldn't necessarily want to accept these as valid dates.

#6 Updated by Woody Gilk about 4 years ago

We cannot use strptime because it isn't available on Windows.

#7 Updated by Woody Gilk about 4 years ago

  • Status changed from Feedback to Closed
  • Resolution set to wontfix

Human Internals wrote:

Do notice that strtotime returns valid dates to other strings such as 'Next Sunday' , '+30 days' etc.
You wouldn't necessarily want to accept these as valid dates.

Those are valid dates though.

I'm closing this as wontfix because there is no better solution presented. Patches welcome.

#8 Updated by Thomas Lété over 3 years ago

  • Status changed from Closed to Feedback
  • Target version changed from v3.0.8 to v3.2.0

Hi !
I reopen this bug because I recently used the php DateTime class which could fit the needs of the validation class.

http://be2.php.net/manual/fr/datetime.construct.php

A quick fix could be :

public static function date($str) {
try {
$date = new DateTime($str);
}
catch (Exception $e) {
return false;
}
return true;
}

Please give me your opinion :)

#9 Updated by Isaiah DeRose-Wilson about 3 years ago

  • Status changed from Feedback to Closed
  • Target version changed from v3.2.0 to v3.0.12
  • % Done changed from 0 to 100

Also available in: Atom PDF