Jump to content

Problem with date format


diegonella
 Share

Recommended Posts

Hi and bienvenido diegonella

You should be able to define format in the field details setting. Once you created the datetime field and save there is more options on top tabs. look for "Details"...

Link to comment
Share on other sites

Thanks, I just tried out. I think the behaviour is a bug or by intention. Ryan would have to confirm this though. I would agree that it is strange to use this way.

No matter what you specify in the details setting. When on page editing it shows info text: "m/d/Y" , but when entering, it is only valid with something like 10-08-2011. If you use the datepicker it works, just not the way you would expect.

The output at template using echo $page->datefieldname; though it outputs as you defined in settings.

Link to comment
Share on other sites

See the note that accompanies the date input format in PW's field settings. It says this:

We recommend using 'd-m-Y' (with dashes) for European dates or 'm/d/Y' (with slashes) for US dates...

What that means is that it has to be a format that PHP's strtotime() function understands. Meaning, you'll want to use dashes rather than slashes for d-m-Y. Here are the date formats strtotime understands: http://www.php.net/manual/en/datetime.formats.date.php

As for the date picker populating the format Y-m-d, it does that because javascript (jQuery) and PHP don't use the same date format strings, so we have to use an ISO-standard date format for something that is automatically populated from a date picker. So we're using ISO-8601 (without time component) which is recognized in the same way by both JS and PHP. As soon as you hit save, it'll show you the date in your preferred format.

So this all works, but there are those caveats. If you guys can think of a better way for us to do it, let me know.

Link to comment
Share on other sites

Ah now I get it, there's "input" setting too. Didn't see that before.

So this makes sense, and works no matter if i enter manually with slashes or dashes.. strtotime does recognize them all.

But using jquery datepicker is not right. There's options to configure ui datepicker. SO if selecting a date it enters in the way you define it. I worked with it a few times and I see no problems with doing it. The way it is now, it populates the field with a standard date like "2011-10-09" which does work and gets transformed when saving, but would be nicer to have it populate the way you defined under input settings.

But maybe I'm getting it wrong with what's behind it in PW. Would strtotime not work when implementing this because it doesn't know which is d and wich is m? Is that the ISO format you're speaking about?

Link to comment
Share on other sites

but would be nicer to have it populate the way you defined under input settings.

I totally agree. But the jQuery dateformats are not the same as the PHP date formats. I don't want to have to ask the user to define the date in two different formats (PHP date, and jQuery datePicker format). If these two were the same, then that would be great, but they aren't:

http://docs.jquery.com/UI/Datepicker/$.datepicker.formatDate

http://www.php.net/manual/en/function.date.php

The only way around it that I can see is to have something to translate one input format to another, or just offering predefined date formats. I decided it would be better to give the user the most flexibility by letting them use any date format, with the compromise of having the datepicker use it's own format. Let me know if you can think of any alternate way to do it.

Note that the link I posted before was outlining formats that strtotime() can recognize for input (and there is similarity to the jQuery formats), but PHP doesn't recognize them for output (unless I've missed something), and we need the output formats to be the same. Input format doesn't matter too much since strtotime() recognizes near everything. :)

Link to comment
Share on other sites

The alternate field would be a great way to use some different format for input, but input isn't the problem… we're lucky to have strtotime() which can recognize nearly any known format. Instead, output produced by jQuery's datepicker is the problem. It needs us to specify a date format in a completely different way from PHP. To make it all work with the same format, both PHP's date() and jQuery's datepicker would have to recognize the same format for output. Because the two don't use the same format strings, the compromise was to just let jQuery's datepicker populate in a predefined format (ISO-8601 or another, it doesn't really matter which). I think that other CMSs resolve this by using predefined formats, and that may be the direction we eventually have to take.

Link to comment
Share on other sites

I'm not sure what you mean with input/output exactly anymore. I think there's a missunderstanding :)

This is exactly what's needed to solve the problem here with datepicker. You can display the date in a user defined format (visible to the user), while have a different format output to a hidden field and use only that to save it to db. I've done it myself in apps, so I see no problem with this approach.

Quoted from jquery ui doc:

"The dateFormat to be used for the altField option. This allows one date format to be shown to the user for selection purposes, while a different format is actually sent behind the scenes. For a full list of the possible formats see the formatDate function"

http://jqueryui.com/demos/datepicker/#option-altFormat

EDIT: ah, do you mean by format, what the user defines in PW field setting like d/m/y isn't compatible with ui datepicker format?

Link to comment
Share on other sites

ah, do you mean by format, what the user defines in PW field setting like d/m/y isn't compatible with ui datepicker format?

Exactly–that's the issue. The alternate field doesn't do us much good just because that's for sending input back to the CMS to be saved, and we have no problem with that at present. The only issue is just in getting datepicker to output the selected date in the same format it does in PHP. While there are ways to solve it, none are particularly simple, and days(s) long projects for visual tweaks to the datepicker aren't at the highest priority at this stage (though eventually will be). :) If there was a simple solution I'd be all for it though.

Link to comment
Share on other sites

I see. I went through this and tried something which works converting php to jquery ui format. Since datepicker doesn't support time output, you gonna use datepicker only for years months days anyway. Thought it's possible to break it, but most reasonable cases work. It wouldn't be perfect but at least a little closer.


<?php

$replace = array(
'd'=>'dd',
'D'=>'D',
'j'=>'d',
'l'=>'DD',
'N'=>'',
'S'=>'',
'w'=>'',
'z'=>'',
'W'=>'',
'F'=>'W',
'm'=>'mm',
'M'=>'M',
'n'=>'m',
't'=>'',
'L'=>'',
'o'=>'yy',
'Y'=>'yy',
'y'=>'y',
'a'=>'',
'A'=>'',
'B'=>'',
'g'=>'',
'G'=>'',
'h'=>'',
'H'=>'',
'i'=>'',
's'=>'',
'u'=>'',
'e'=>'',
'I'=>'',
'O'=>'',
'P'=>'',
'T'=>'',
'Z'=>'',
'c'=>'',
'r'=>'',
'U'=>''
);

$str = "Y.m. l H:i:s";
$out = str_replace(array_keys($replace),$replace,$str);
$out = preg_replace("/^((?![a-zA-Z]).)+/", '', $out);
$out = preg_replace("/((?![a-zA-Z]).)+$/", '', $out);

echo "PHP to jQ UI dateformat: '$str' => '$out'<br/>";

Link to comment
Share on other sites

Soma, this looks like it might be exactly what we need. Thanks for your efforts in putting this together. I'm offline for the holiday weekend but will get this working with the date inputfield early next week. Thanks again.

Link to comment
Share on other sites

  • 4 weeks later...

Few more issues with current implementation:

If you add date in wrong format, after saving it says

Change: datefield

But silently keeps the old value. Also php format values (like:  j.n.Y H:i) doesn't say anything for normal people (ie. non-coders), so I would leave those out.

I actually would like to see some predefined solution here. Or maybe some kind of "hybrid", like Zoho invoicing has:

post-79-132614279815_thumb.png

post-79-132614279818_thumb.png

post-79-132614279823_thumb.png

Link to comment
Share on other sites

I agree with all this. Though the languages module lets you specify date formats, and I was thinking the InputfieldDate would be able to pick up the date formats from the language definitions. But still thinking about this one. I didn't want to put a lot of work into the date Fieldtype until we had the multi language stuff down because it seems like the two would ideally go together.

Link to comment
Share on other sites

But silently keeps the old value. Also php format values (like:  j.n.Y H:i) doesn't say anything for normal people (ie. non-coders), so I would leave those out.

Tested this little bit more. It actually falls back to current date, if that is set ("If checked, this field will hold the current date when no value is entered.").

Great to know that language support has dates covered, great news!

Link to comment
Share on other sites

  • 4 months later...

Have we decided what to do with this? Trying to get dynamic JS/PHP date conversions or going with predefined date formats?

Currently it is impossible to get date picker to work with Finnish date format: d.m.Y - it messes up months and days.

Link to comment
Share on other sites

When you populate a date into the field via the date picker, it should populate it in with this ISO format: YYYY-MM-DD. This is a format recognized by both strtotime() and jQuery's datepicker. So once you hit save, it should be in correct Finnish format. It sounds like that's not what it's doing in your case? Can you describe more?

We will probably go with predefined date formats that both PHP and jQuery can recognize, as soon as there's time to get into it.

Link to comment
Share on other sites

It does. Problem comes later on. This is the scenario:

  1. I choose second day from January this year from datepicker: 2012-01-02
  2. I save page, 2012-01-02 is rightly formatted to Finnish date02.01.2012
  3. I click datepicker again. Datepicker believes it is first day in February...
  4. This leads to a mess :)
Link to comment
Share on other sites

  • 4 weeks later...

Thanks for the good description on the issue there. I finally understand it and have fixed it (I think). But I'm making some other updates to the Date Inputfield so going to be committing them all together.

  • Like 1
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...