Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Problem with date validation

posted by aris 9 years ago

In my 'groups' table (PostgreSQL 8.4.3) I have defined two date type columns:

...
start_date date,
end_date   date,
...

When creating a new record, a var_dump($groups) just after $group->populate() shows that both date values (in the form d/m/Y, i.e. '21/05/2010') are already there. However, $groups->validate() is throwing an error:

The following problems were found:
    * Start Date: Please enter a date
    * End Date: Please enter a date

So it seems both dates are not being recognized as such by the validation process. What am I missing?

Flourish uses strtotime() for date parsing. That function requires the date be in a US English format, which would require m/d/Y.

To remedy this issue, all date and time parsing goes through fTimestamp, which allows for localization: fTimestamp#Localization. You will need to write a function that translates to the US format, and you'd probably want it to be user-specific.

posted by wbond 9 years ago

Thanks for your reply!

I have tested the example function in fTimestamp#Localization and it seems it has a problem.

Using the expresion #(
d{1,2})/(
d{1,2})/(
d{2}|
d{4})# as shown in the example, the year is always taken as 2-digits number, because d{2} takes precedence over d{4}. So the resulting $matches array for a date such as 26/05/2010 is:

array(4) { 0 => string(8) "26/05/20" 1 => string(2) "26" 2 => string(2) "05" 3 => string(2) "20" }

then returning a wrong final result of 2020-05-26.

Changing the order in the year part of the expression #(
d{1,2})/(
d{1,2})/(
d{4}|
d{2})# seems to do the trick:

array(4) { 0 => string(10) "26/05/2010" 1 => string(2) "26" 2 => string(2) "05" 3 => string(4) "2010" }

A.

posted by aris 9 years ago

Thanks for the heads-up! I added begin and end anchors as an alternate solution.

posted by wbond 9 years ago