Date formats…
-
I am wondering if there is a reason why you don’t allow for a different date format for the “text_date” field as you do on the other Unix timestamp fields. I was thinking I might want to enter it as Y/m/d into the database instead of your default.
Thanks
-
Saving a unix timestamp in the database is preferred since it’s just one big integer which is easily converted using the php “date” function.
For example, let’s convert: 1411150326
<?php // Get unix date $some_date = get_post_meta( get_the_ID(), 'my_key', true ); // Convert unix date $some_date = date( 'Y/m/d', $some_date ); // Print converted date echo $some_date; ?>
Would echo
2014/09/19
You can read more about date functions here: https://php.net/manual/en/function.date.php
Hope this helps.
Hey, thanks for your response.
I was actually using text_date and it was even working for my comparisons pulling custom post types with wp_query on the front page (Events page).
However, I ran into some ordering issues when doing filters for next_post_links off of single posts. Thus, I was looking into storing dates in the Y/m/d on the backend. So, in the end, I may start using timestamps instead as I have read they sort and order better.
In these sort of cases, and in connection with your plugin would use use the Date and time combo, or do you feel it might be more flexible to separate the two? Or no difference?
WPWanderer, I’m curious if you’ve had any new developments with this or if you still need some help with it.
Let me know and we can go from there ??
Hey Michael, nice of you to ask. I didn’t follow-up as I felt in fairness it went beyond the support of your plugin.
I had everything working just using text_date. This includes pulling a custom post_type by date and ordering by time with a custom query. I got into thinking I should be using timestamps when I wanted to do filters for the previous_post links and they weren’t working.
Anyways, after much pondering and stackoverflowing I think I might just go back to text_date and tweak the global query for the previous_post links.
That being said, I would still like to have the date entered into the database as Y/m/d instead of m/d/Y. Where in your plugin is this defined? Possible to make the change?
You should be able to pass in a ‘date_format’ argument for your date fields and that format would be used instead of the default.
Example:
array( 'name' => 'Test Date Picker', 'desc' => 'field description (optional)', 'id' => $prefix . 'test_textdate', 'type' => 'text_date', 'date_format' => 'Y/m/d' )
Hi. I have used your previous version and noticed that the date_format was not an option for ‘text_date’ . That actually was my original question above.
I just tested again on this new version and it looked like it also doesn’t work when I look at the value in the database. Still reads the default.
Just confirming. Thanks.
It wouldn’t alter the existing ones, you’d need to re-save those. New ones should though. If not, then we have a bug.
Yeah. I did. I don’t see any affect of date_format on the text_date type and how it is entered into the database. The value will always be mm/dd/yyyy in the database.
You may want to do a quick check for yourself, but that is what I am seeing.
Until further notice, I’m having to list this as a bug ??
Hopefully we’ll have it finished and working as expected once out of beta, as you can see it’s listed as.
Great. No worries.
Just to add to this, it would be nice if one could determine the display date on the front end. In my case, being in the UK, we display the date as d/m/Y. I use the text_date_timestamp and the display is m/d/Y regardless of setting ‘date_format’ => ‘d/m/Y’ for the field which can be confusing to editors.
Should be completely possible, just a matter of running the saved value through some extra PHP functions manually.
It looks like that one should be saving the value as a timestamp, ala strtotime(), you’d just need to pass it through date() with the appropriate format in your templates, instead of just calling and echoing the value as-is.
Hi Michael,
Thanks for your reply – sorry, I used ‘front end’ here by which I meant the admin screen (i.e. not the code site of things). For that I believe what would need to be able to set a jqueryui parameter. I just noticed a problem with this. I entered a date via the date picker, 3rd December 2014 which is displayed as 12/03/2014. Here are the field settings
array(
‘name’ => __( ‘Route date’, ‘cmb2’ ),
‘id’ => $prefix . ‘date’,
‘type’ => ‘text_date_timestamp’,
‘date_format’ => ‘d/m/Y’
)When this is displayed after saving and then reloading the post, it shows up as 03/12/2014. This then obviously causes a problem with the date picker as it the shows 3rd March. Any solution to this?
From what I can see with my own testing, using the field above, it will save and initially display in the format specified in the
date_format
portion, however, once jQuery UI gets invoked again, it’s reverting back to the m/d/Y version. This is something that is being hardcoded in the js for the datepicker. Thankfully it’s only for the duration of selecting a new date. However, I can still see how it’d be confusing to visually switch back and forth.So, with that, I’m going to submit an enhancement ticket to localize the jQueryUI format used, based on user settings. Hopefully Justin or someone else can come up with a working fix for that so that it’s consistent the whole time. Worst case, it may come down to a filter that can be added to change as necessary, in the event someone doesn’t want to use their WP setting format.
- The topic ‘Date formats…’ is closed to new replies.