Set altFormat for jQuery datepicker field
When using the date field in Carbon Fields, it will accept most of the standard arguments for the datepicker options, but it ignores the altFormat option entirely and saves the field as yyyy-mm-dd. It would be very helpful to be able to show the user the standard datepicker but save the value as a UNIX timestamp. Any thoughts?
https://api.jqueryui.com/datepicker/#option-altFormat
Thank you for the input @smilingpeanut ,
In 2.0, the date/time/datetime fields always store in the standard Y-m-d H:i:s MySQL format while the display format can be configured freely (e.g. users will see 12/31/2017 but 2017-12-31 will be stored in the database; I believe the storage format is customizable as well).
Thanks for the reply @atanas-angelov-dev. In the current 1.6 version, is there a way to achieve this scenario?
Date user sees: 2017-06-06 10:00am Date stored in database: 1497484800
There is no built-in way but extending Date_Time_Field and overwriting the set_value_from_input() (convert user format to db format) and to_json() (the reverse) methods should enable you to achieve it.
@smilingpeanut There is an example at the bottom of this page that shows exactly how to save the date as a timestamp in a separate field (which you can then query on, etc..). https://carbonfields.net/docs/containers-post-meta/ <-- It would make a ton more sense if this code sample was on the actual date/datetime docs page by the way, would have saved me a lot of time! I personally used DateTime::createFromFormat instead of strtotime to convert the Carbon Fields value to a timestamp.
Here's the code from the page linked to above:
add_action('carbon_after_save_post_meta', 'crb_after_save_event');
function crb_after_save_event($post_id) {
if ( get_post_type($post_id) !== 'crb_event' ) {
return false;
}
$event_date = carbon_get_post_meta($post_id, 'crb_event_date');
if ( $event_date ) {
$timestamp = strtotime($event_date);
update_post_meta($post_id, '_crb_event_timestamp', $timestamp);
}
}
@richardbuff Thank you! I missed that somehow.