Jump to content

Date input output format?


bwakad
 Share

Recommended Posts

The goal is to have people enter a birthdate, and in the end only displays the current age on their profile.

Also people should be able to search on age.

So I have 3 things to do this:

Format: upon entering in front end - is controlled by regex. This is just the format.

Validation: before saving - done by php function checkdate(). This is to see if date excist. Leapyears and dd=32 etc.

Age: after validation and saving - calculation is done by php function datetime(). It takes formats from different timezones into consideration.

But how to implement this in PW? I know there is a DateTime field. But the output and input formats puzzle me.

I need mm dd yyyy where the validate function already take the order of those segments into consideration,

  and either with or withouth  "-" or "/".

Does the backend formats affect the frondend inputs? And how to search on this field if I just need the Age (years)...

Link to comment
Share on other sites

The input formats are the format's which are used by the backend input fields. The output format's are the ones you get if you call "$page->date". There you should use the date format you're using the most. To get other formatting just use this: 

date("DATEFORMAT", $page->getUnformatted("date"));
Link to comment
Share on other sites

so that means I would need to set output format to "none" since I am calling $page->getUnformatted("date"),

take the value from that, apply php funtion datetime(), to output "xx" as years.

Could use a number field for that I gues.... example: number would be 12031980.

Just realized I see integer as field type.

Link to comment
Share on other sites

Another thing accomplished!

I now use an integer field on frontend - but still need to sanitize this as integer...

I use a pattern for this input in format yyyymmdd. Since I also do server side validation for the date, I do not have to worry.

For the PW part I have this code below - and if interested, for the functions you should go to phppro.org - they have a clean and understandable layout of code examples which are easy to follow.

The $testvalue is as the name suggest, a test.

Next thing to consider is, if it is wise to do this calculation on a per page view, because asigning  the $testvalue as a static value will lead to unaccurate age display.... how would PW think about that ?

// on submit:

       // no input received, return error
       if(empty($input->post->birthdate)){
             $errorMessage .= "<li><b>Birthdate</b> is required.</li>";
       }

      // birthdate is invalid, return error
       elseif(validateDate($input->post->birthdate, 'YYYYMMDD') === false ) {
             $errorMessage .="<li><b>Birthdate</b> is not valid</li>";            
       }
                        
       // birthdate is valid, do calculation
       elseif (validateDate($input->post->birthdate, 'YYYYMMDD') === true) {
             $dob = $input->post->birthdate;
             $tdate = date(Ymd);
             $testvalue = getAge($dob,$tdate);
       }
Link to comment
Share on other sites

so that means I would need to set output format to "none" since I am calling $page->getUnformatted("date"),

No. The output format is only used on formatted output like "$page->date" while getUnformatted() returns the raw date as timestamp. These can easily be used by the php date functions. So there's no need to hijack the integer field to save dates.

  • Like 1
Link to comment
Share on other sites

Under the curcumstance, that the frontend doesn't only display one single dateformat, the advantage of using my suggestion over "none" as output format is the display in the admin backend. PageLabels or PageTables benefit from displaying a readable date instead of the raw timestamp. All the actuall input fields of the backend are, of course, controlled via the input format. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...