Hi @architbjoshi,
I’m guessing by start date and end date fields you’re referring to two date fields you’ve created.
Unfortunately there’s no such logic built into ACF.
Your only option is to insert your own script on your admin page with which you update the datepicker options.
You can use this filter to add your custom script file:
http://www.advancedcustomfields.com/resources/acfinputadmin_enqueue_scripts/
And then you’ll need to check when either start or end date input has been changed and modify the other field accordingly.
//To set a minimum date (back in time)
jQuery( '.acf-field-UNIQUEID .acf-date_picker input.input' ).datepicker( 'option', 'minDate', date.format() );
//To set a maximum date (forward in time)
jQuery( '.acf-field-UNIQUEID .acf-date_picker input.input' ).datepicker( 'option', 'maxDate', date.format() );
Hi @roura356a
You say you have ACF and the repeater addon.
Have you considered getting the ACF Pro plugin instead? If you’ve purchased the repeater addon you should be eligible for a free upgrade from your account page.
I’ve used date pickers inside repeaters with ACF 5 (pro) and it has worked for me so I think that would solve your problem (and give you more and better functionality).
This worked for me:
// disable acf css on front-end acf forms
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
wp_deregister_style( 'acf' );
wp_deregister_style( 'acf-field-group' );
wp_deregister_style( 'acf-global' );
wp_deregister_style( 'acf-input' );
wp_deregister_style( 'acf-datepicker' );
}
Hi @th219,
When trying to create a PHP datetime object using the DatePicker field, it is recommended to use the following functions:
$date = DateTime::createFromFormat('Ymd', get_field('exhibitionstartdate'));
echo $date->format('d-m-Y');
I hate to drag a two year old topic to the surface, but I’m wondering the same thing? I’m trying to import a large CSV with some dates and am unable to get the dates to show up properly in my Datepicker fields.
Currently, my dates are formatted in the CSV so j/m/Y
, for example: 7/5/2003
.
How should they be formatted in the CSV to be imported properly so ACF can read and display these dates properly?
Edit
Ok, that didn’t take too long to figure out. I looked in the database when setting the fields correctly through the ACF form, and saw that the dates were stored like YYYYMMDD
or 20030507
. Converting the dates in my spreadsheet (Google Drive, via `Format -> Number -> More Formats -> More date and time Formats…) and everything’s working perfectly.
are you sure about the compare?
it should only show dates that are currently ongoing (have a startdate today or inside the past, and a enddate of today or the future.)
you could do it after query with php with code like that:
<?php
$today = date('Ymd');
$start_date = get_field('start_date');
$end_date = get_field('end_date');
if ((strtotime($today) >= strtotime($start_date)) && (strtotime($today) <= strtotime($end_date))) { //echo or something you like to do with the dates
}
?>
else, if you still need/wish to work with meta_value i can help you only with this info:
date of datepicker inside DB is always build like 20150513 year month day without spaces, no matter what input or output format you give the date field with acf settings for that field.
i am not sure if your $today has the same format/value. hope that help you to solve your problem.
of your example dates it could only show the first (even if you would correct the 2051 to 2015)
Hi @spinline
It should not bring about any error. The logic in your code is correct.
Hmm… You need to echo the value of get_field('start_date')
when the date_picker field does have a value to find out what it returns. In your case, it seems to be returning something.
Thawnks Onyx808, works great! For those looking how to month or year, here is the list of formats – http://php.net/manual/en/datetime.createfromformat.php.
So if I input 10/24/2015 into the date picker, here’s the code to display “Saturday, October 24, 2015”.
<?php echo $dates->format('l');
echo ', ';
echo $dates->format('F');
echo ' ';
echo $dates->format('j');
echo ', ';
echo $dates->format('Y');?>
Hi @pagol
ACF does not have a time picker field type, it only has the date picker field type.
Check out this plugin: https://wordpress.org/plugins/acf-field-date-time-picker/ May be it could be of some help.
For the date format, you could set the return format to D M j Y
for the date picker return option.
Hi … great plugin … did you manage anything with the multi date selecting from the datepicker field?
Thanks…
Looks like it has been removed. Looks like this plugin supports 5.x though. Just tested it, and it’s working fine.
I’ve been having some trouble with this as well. I have a “Upcoming Events” page, and a “Past Events” page. Each event has a custom field called “event_date”.
I want to create a loop that displays all of the events greater than today.
From what I’ve gathered in the three links above, I would put this in my functions.php file:
// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'event_type' ) {
$event_date = get_post_meta($post_id, 'event_date', true);
if($event_date) {
$dateparts = explode('/', $event_date);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
Then I would add something like this to my page template:
<?php
$today = time();
$args = array(
'post_type' => 'event_type',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$event_type = $query->posts;
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Right now that’s not turning up any results. My post-type is called “event_type”, and the key is “event_date”.
Any thoughts on where I’m going wrong?
for populate a filled out value at frontend you can do something like that:
<?php
/*
* Create PHP DateTime object from Date Piker Value
* this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
*/
$date = DateTime::createFromFormat('Ymd', get_field('date_picker'));
echo $date->format('d-m-Y');
?>
official Datepicker Documentation
or did you wish to prefill a datepicker field?
Hello,
my choosen datepicker (FIELD TYPE) is not selected anymore, but seems to work. And German language is destroyed, too.
Cheers,
Denis
Mh, maybe it is something how Gravity forms fills out the post_thumbnail field and/or saves posts and custom fields? I managed to fill other (text-based) fields with Gravity forms frontend posting, but so far the image field fails (had a similar problem with a date picker field – even though it had the same format, it never reached the actual ACF field when saving a post with Gravity forms). So maybe the complication is somewhere along GF’s creation of posts.
Your code is working correctly for me.
Are you sure that you have your date picker returning the date in the Ymd
format? (See the attached screenshot of the ACF interface where the return format is set.) You can just echo get_field('event_date')
to check the format you’re getting back.
Alternately, you could do it in procedural style this way, which should work no matter what format is being returned:
<?php $date = strtotime(get_field('event_date')); ?>
<span><?php echo date('d', $date); ?></span>
<span><?php echo date('M', $date); ?></span>
I solved the issue using this post…
http://support.advancedcustomfields.com/forums/topic/using-datepicker-to-display-posts-on-a-page/
Right now I’ve got the following working:
acf_local()->get_field_groups()
.acf_get_local_fields( $fieldgroup_key )
.foreach ( $groups as $fields ) : foreach ( $fields as $field_key => $field_data ) : printf( ... ); endforeach; endforeach;
action=""
).update_field( $field_key, $value, 'options' )
.Currently this works fine for non-JS infused fields, such as selects, text fields and so on. I’m working on getting the color picker working, which in the end should use a hidden text field anyway.
Is there a more correct way to do this or should I wrap this into a plugin of sorts?
I know this post is over a year old now, but I recently had a similar problem to kylerumble and wanted to provide a solution on the off-chance that someone else can use it.
The problem: A company takes part in several events, some of which span multiple days, and wants to highlight them across two sections on their website: Upcoming and Past. The Past section shows all events from the previous year up until (the relative) yesterday with the exception of multi-day events. If an event spans multiple days, it continues to be shown in the Upcoming section until the current date is after the last day of the event.
Like kylerumble, I didn’t want the editors to have to fill in the date twice if the event only lasts a single day.
The solution:
I made three ACF fields – “Multi-day Event” (True / False field), “Start Date,” and “End Date” (both Date Picker fields) – and set a conditional around the Start Date field so it only displays if the “Multi-day Event” field is checked.
Using Jonathan’s last function as a starting point, I used the save_post action to update the field when necessary:
// run AFTER ACF saves the $_POST['fields'] data to database
add_action('acf/save_post', 'single_day_event', 20);
function single_day_event( $post_id ) {
// Ensure we have ACF fields on the post.
if( empty($_POST['acf']) ) {
return;
}
// Set up our variables with the respective ACF field keys
$multi_day_event = $_POST['acf']['field_123'];
$start_date_field = $_POST['acf']['field_456'];
$end_date_field = $_POST['acf']['field_789'];
/* Update the Start Date field value with the End Date field value if any of the following are true:
1. The Start Date field is empty
2a. Multi-day Event is empty and the Start Date doesn't equal the End Date
2b. Multi-day Event's value is 0 and the Start Date doesn't equal the End Date (This is in case an event is ever set to Multi-day and then later changed to a single day)
*/
if( ( $start_date_field == '' ) || ( ( $multi_day_event == '' || $multi_day_event == 0 ) && ( $start_date_field != $end_date_field ) ) ){
update_field('field_456', $end_date_field, $post_id);
}
}
Cheers!
WP All Import support team are awesome. We’ve submitted 2 issues and they replied within 24 hours.
The first one was solved in 24 hours, the issue was to add support for validated_field type (http://wordpress.org/plugins/validated-field-for-acf/).
The second one, is about date_picker in repeater field. Though it took a bit longer but they work and fixed it in less than 10 days, just before Dec 25.
Now for the plugin itself, we found version 4 much better. With above issues solved WPAI v4.0.7 is now working with ACF v4.3.9 + ACF:Repeater Field v1.1.1 + ACF:Validated Field v1.3.1 + WPAI:ACF Add-on 3.0.2.
Also solved a problem where the value in the colorpicker (not the input field) gets nulled after each post update.
Above code updated.
Kudos to you guys for trying to solve this. I tried above solution but can’t get it to work 100%.
I managed to solve to things:
See my revised jquery code below:
<script>
jQuery(document).ready(function($){
if ($('.acf-color_picker').length) {
$('.acf-color_picker').each(function() {
$( this ).iris({
mode: 'hsv',
palettes: ['#e40571', '#4b63a4', '#ffcb05', '#fff', '#000'],
change: function(event, ui){
$(this).find('.wp-color-result').css('background-color', ui.color.toString());
$(this).find('.wp-color-picker').val(ui.color.toString());
}
});
});
}
});
</script>
HOWEVER
I use Flexible Content for some of my color picker fields and those are not initialized on page load. Therefore above hack doesn’t affect them.
I think the best solution is to do these changes when ACF initializes.
The only way i could accomplish this was to change the file: /wp-content/plugins/advanced-custom-fields-pro/js/input.min.js
Which isn’t good since it will get removed on updates.
I would really appreciate if Elliott or someone could help to solve this via a function or a plugin.
Best regards
Anton
Hey Jacoba10 –
I believe I found and used the jquery UI datepicker… it appeared that is what ACF uses and I don’t think I had to re-declare the UI jquery library either. Hope this helps!
Hi @Rai Uriarte
1. You can get the date picker value like so: get_field('field_name')
http://www.advancedcustomfields.com/resources/get_field/
2. You can get the current date like so:
http://php.net/manual/en/function.date.php
3. You can compare them like so:
http://stackoverflow.com/questions/8722806/how-to-compare-two-dates-in-php
Hope that helps
Cheers
E
Yea I just decided to move to using the ACF date picker. Thanks.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.