Just updated the docs:
http://www.advancedcustomfields.com/resources/field-types/date-picker/
Hi @tebbott
Using the strtotime function should work like this:
<?php
$date = get_field('date');
// $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
// format date
echo date('d/m/Y', $time);
?>
Good luck!
Cheers
E
Any chance that someone is able to take the code in the first reply, and relate it to the code in the original ticket? I have the same issue (shared server and unable to update the PHP version), so need to be able to output the date in a standard dd/mm/yy format.
Hi @tigerfish
Looks like you need to add in a meta_query arg to the query.
To get you started, please read the guide for filtering posts:
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
After you have read and understand the above, please have a search around for “query posts after date custom field” – you should find plenty of threads on google!
Cheers
E
In my add on, date and time picker field, date and time is stored as a timestamp.
When a user open a post to edit the date and time field, I use format_value() to convert the timestamp to the format the user set when he created the field.
What should I use to convert the timestamp to the correct format in revisions ?
edit: @elliot ?
Hi @Dave Burrows
If you see a white screen of death, you have a PHP error.
Please edit your wp-config.php file and turn on DEBUG MODE. This will reveal the error which you can report back with.
Thanks
E
Thanks! Never thought about approaching it that way. That’s what I’ll do for now, however, it would be great have a default date value built into later iterations of the plugin. I suppose I can’t complain…this plugin is a godsend. Thanks again!
Thank you for the answer @elliot.
We are doing a history website and most of the important people in the 20th century are born before 1913 🙂
Hi @Deian
If you select a date, the 100 year range should update, also, yes, you can type in the year.
Perhaps I need to add this in as an option in the backend, however, it’s quite rare that a user would need a larger dropdown that 100 values.
Just a suggestion, but would it be possible for you to enter hypothetical date for the release of the film but also have a check box field for “To Be Determined”. That way, the entry will still sort in your lists but the date that you insert will be replaced with “To be determined” in the page view?
<span class="release_date">
<?php if get_field('tbd'){?>
To be determined
<?php }else{
the_field('release_date') };
?>
</span>
This might only be a duct-tape solution and there is probably a better answer to this, but it should work for the time being.
Hey Elliot,
I’m thinking that I don’t since I can’t get it to work. 🙂
Hi @aaronrobb
Just to confirm, do you full understand the difference between the JS format string and PHP format string?
New question/problem along these lines.
Just imported a CSV with a date field, in the format of ‘yymmdd’, and now the Datepicker doesn’t show anything on the admin editor page. Just a blank field. Save value is set to yymmdd, display is set to dd/mm/yy.
Just tested with a text field instead and the yymmdd value shows up. Any idea why the datepicker field will just show blank?
The save format is set to yymmdd
Looks like for some reason, I have to make the CSV’s date to be yyyymmdd and the save format to just the yymmdd which really doesn’t make sense, but it seems to have worked.
Hi @serviceweb
This is a question for the developer of the time / date picker. Please use their github support.
Thanks
E
Hi Elliot,
Thanks for answering.
My english is not the best, but i try to explain my problem.
I add a screenshot, here you can see the crazy order of my posts.
Yes, i’ve used your “how to” guide, but no matter what approach I use, it stays the same result.
The posts are not in order and the aim is to sort it like newest -to-> older ones. The newest post starts from 03.06.2013 and the oldest and last post have to be the 01.09.2011.
I dont know, whats going wrong, every post has a date field (Date Picker, save and display format: dd.mm.yy). When i remove the “meta_key” and “orderby” arguments, it changes the order. So i think the query reacts to this arguments …but nevertheless something is going wrong.
Thanks for trying to help me! 🙂
Okay, thanks for my friend Matt we managed to work this out.
Basically, my PHP skills failed me a little as $date_passed
was defined in an if statement, thus it’s not always defined and you are getting PHP notices when it is not defined.
So, he suggested I either define $date_passed
to something (null) or use isset or empty to check whether it has been defined.
So adapting my code to below, worked:
$date_passed = null;
if ( $end_date_passed_check->format('Ymd') < date('Ymd') ) {
$date_passed = 'date-passed';
}
I hope this post proves to be useful in applying specific classes to events that have passed using the ACF DatePicker.
Full code as follows:
<?php get_header(); ?>
<?php if ( ! have_posts() ) : ?>
<?php endif; ?>
<div class="container_12">
<?php
while ( have_posts() ) : the_post();
$end_date_passed_check = DateTime::createFromFormat('Ymd', get_field('event_end_date'));
$date_passed = null;
if ( $end_date_passed_check->format('Ymd') < date('Ymd') ) {
$date_passed = 'date-passed';
}
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="grid_3">
<div class="each-programme-content <?php echo $date_passed; ?>">
<a class="each-programme-content-info" href="<?php the_permalink(); ?>">
<?php if ( get_field('type_of_event') ) : ?>
<span class="type-of-event"><?php the_field('type_of_event'); ?></span>
<?php endif; ?>
<?php if ( get_field('event_start_date') && get_field('event_end_date') ) : ?>
<?php $start_date = DateTime::createFromFormat('Ymd', get_field('event_start_date'));
$end_date = DateTime::createFromFormat('Ymd', get_field('event_end_date')); ?>
<h4 class="date"><?php echo $start_date->format('d'); ?>—<?php echo $end_date->format('d F'); ?></h4>
<?php elseif ( get_field('event_start_date') ) : ?>
<?php $start_date = DateTime::createFromFormat('Ymd', get_field('event_start_date')); ?>
<h4 class="date"><?php echo $start_date->format('d F'); ?></h4>
<?php endif; ?>
<h4><?php the_title(); ?></h4>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="clear"></div>
<?php get_footer(); ?>
Hey @rdck,
Thanks for getting back to me so fast. That looks very promising and I’ll try have a look in to it myself but let me know if/once you get something working 🙂
Thanks buddy.
Hi @rootshift,
Just working through this with @elliot so close to a solution.
Once this has been worked out, I’ll post up the solution.
But basically, you should be able to do this for your button:
<?php
$event_end_date = DateTime::createFromFormat('Ymd', get_field('your_event_end_field'));
if ( $event_end_date->format('Ymd') < date('Ymd') ) { ?>
<a href="#">Volunteer link</a>
<?php } else { ?>
Volunteering no longer available
<?php } ?>
Hi mate,
Just wondering if you got this sorted? I want to create the same kind of functionality.
Basically I have an events page, where people can volunteer. We have a volunteer link, but obviously we don’t want this here if the event has passed.
So we need some kind of conditional logic saying, display the volunteer link, if the date has passed, dont. display none maybe.
Did you get a working solution? I would be grateful to hear how you went about doing this.
Many thanks
Hi @elliot,
Many thanks for your reply.
I am testing this by echoing out both the current date and get_field('event_date_date')
with the format Ymd
with each event to compare using:
echo $end_date_passed_check->format('Ymd');
echo '<br/>';
echo date('Ymd');
These all seems to be correct, but what it seems to be doing is when the first if statement is true, then it applies it to the rest of the elements. I think this might be a while loop error, but maybe you can help?
Here is what my output looks like http://goo.gl/xW3pwT
Many thanks,
R
Hi @rdck
Have you tested that get_field('event_end_date')
returns a value?
Hi @aaronrobb
The date picker will use the save_format option to load and save data to.
What is the save_format option set to?
Looks like it’s to do with the fact it’s in a while loop?
while ( have_posts() ) : the_post();
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.