Title: [Plugin: Advanced Custom Fields] Sorting by date picker
Last modified: August 20, 2016

---

# [Plugin: Advanced Custom Fields] Sorting by date picker

 *  Resolved [rjpinney](https://wordpress.org/support/users/rjpinney/)
 * (@rjpinney)
 * [14 years ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/)
 * I’m using the date picker to set the date for a custom post type (events) and
   have it set up to order these posts by the custom date field. From the documentation,
   I have the date output format set to ‘yy mm dd’ which allows the sort t work 
   properly.
 * The problem is that it’s difficult for people to read the date in that format
   on the front end and so I want to re-format after the sort to read, for example,
   01 May 2012.
 * Alternatively, if there is a way of ordering by date when the date is formatted
   dd M yyyy then that would be ideal!
 * Any help appreciated!
 * [http://wordpress.org/extend/plugins/advanced-custom-fields/](http://wordpress.org/extend/plugins/advanced-custom-fields/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Stian Andreassen](https://wordpress.org/support/users/stiand/)
 * (@stiand)
 * [14 years ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2721975)
 * If your date format is **yy/mm/dd**, you can convert it to a UNIX timestamp with
   this function:
 *     ```
       function datepicker_to_unix( $date ){
       	$dateparts = explode('/', $date);
       	$unixtimestamp = strtotime(date('d.m.Y', strtotime($dateparts[1].'/'.$dateparts[2].'/'.$dateparts[0])));
       	return $unixtimestamp;
       }
       ```
   
 * Use like this:
 *     ```
       $date = get_field('my_date');
       echo date('d F Y', datepicker_to_unix($date ));
       ```
   
 * This will display the date like **dd M yy**; 01 May 2012
 *  [zaharamh](https://wordpress.org/support/users/zaharamh/)
 * (@zaharamh)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722011)
 * Just what I needed! Thanks.
    This is a GREAT plugin!
 *  [ruhelamin](https://wordpress.org/support/users/ruhelamin/)
 * (@ruhelamin)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722023)
 * Hi. Im looking to create an upcoming events box in the sidebar but haven’t had
   any luck figuring out how to do this.
 * I have a custom post type called events and am using ACF plugin to create a Start
   Date field, among others.
 * I need the typical functionality where the list is sorted by the Start Date field
   and an event automatically gets off the list when it’s gone past the start date
   of that event.
 * Hope someone can help with this. Thanks.
 *  [Stian Andreassen](https://wordpress.org/support/users/stiand/)
 * (@stiand)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722024)
 * Ok, the easiest way is to save your custom field ‘startdate’ as a unix timestamp.
   To do this, add the following to your theme’s `functions.php`:
 *     ```
       // CREATE UNIX TIME STAMP FROM DATE PICKER
       function custom_unixtimesamp ( $post_id ) {
           if ( get_post_type( $post_id ) == 'events' ) {
       	$startdate = get_post_meta($post_id, 'startdate', true);
   
       		if($startdate) {
       			$dateparts = explode('/', $startdate);
       			$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, to get the next five events starting today, use:
 *     ```
       $today = time();
   
       $args = array(
       	'post_type' => 'events',
       	'posts_per_page' => 5,
   
       	'meta_query' => array(
       		array(
       			'key' => 'unixstartdate',
       			'compare' => '>=',
       			'value' => $today,
       		)
       	),
   
       	'meta_key' => 'startdate',
       	'orderby' => 'meta_value',
       	'order' => 'ASC',
       );
   
       $query = new WP_Query( $args );
       $events = $query->posts;
       ```
   
 *  [ruhelamin](https://wordpress.org/support/users/ruhelamin/)
 * (@ruhelamin)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722025)
 * Thanks stiand. I couldn’t get this code to work… maybe i was doing something 
   wrong. But thanks to the info from the code snippets you gave, i found similar
   solutions, which eventually after tweaking and testing, i’ve managed to get it
   working now.
 * Thanks very much for the help.
 *  [ssalgado](https://wordpress.org/support/users/ssalgado/)
 * (@ssalgado)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722030)
 * Thanks stiand, I am having trouble converting the timestamp from ‘Ymd’ to ‘d.
   m.Y’
 * The function you wrote seems to work but it pulls the wrong value. All my events
   are pulling the date “January 01, 1970”
 * Any idea what I could be doing wrong?
 *  [wearepapertiger](https://wordpress.org/support/users/wearepapertiger/)
 * (@wearepapertiger)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722031)
 * ACF will only let you sort by date s long as you use the following default date
   format: yy_mm_dd (as mentioned here: [http://www.advancedcustomfields.com/docs/field-types/date-picker/](http://www.advancedcustomfields.com/docs/field-types/date-picker/))
 * you would need to alter stiand’s code for
 * > functions.php
 *  to the following:
 *     ```
       function datepicker_to_unix( $date ){
       	$dateparts = explode('_', $date);
       	$unixtimestamp = strtotime(date('d.m.Y', strtotime($dateparts[1].'/'.$dateparts[2].'/'.$dateparts[0])));
       	return $unixtimestamp;
       }
       ```
   
 * Where **$dateparts = explode(‘/’, $date);** becomes **$dateparts = explode(‘_’,
   $date);**
 * That did the trick for me. Then you can use stiands following code and it will
   work:
 *     ```
       $date = get_field('my_date');
       echo date('d F Y', datepicker_to_unix($date ));
       ```
   
 *  [Corneliatt](https://wordpress.org/support/users/corneliatt/)
 * (@corneliatt)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722042)
 * Ok, thanks for your help. I’ve solved it!
 *  [mjrwebdesign](https://wordpress.org/support/users/mjrwebdesign/)
 * (@mjrwebdesign)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722049)
 * This post has been really helpful and although it has been solved I wanted to
   add that the below is working for me without the need for the datepicker function
 * ` $date = date_create(''.get_field('driver_start_date').'');
    echo date_format(
   $date,'d F Y');

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘[Plugin: Advanced Custom Fields] Sorting by date picker’ is closed to
new replies.

 * ![](https://ps.w.org/advanced-custom-fields/assets/icon.svg?rev=3207824)
 * [Advanced Custom Fields (ACF®)](https://wordpress.org/plugins/advanced-custom-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/advanced-custom-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-custom-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-custom-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-custom-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-custom-fields/reviews/)

 * 9 replies
 * 8 participants
 * Last reply from: [mjrwebdesign](https://wordpress.org/support/users/mjrwebdesign/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker/#post-2722049)
 * Status: resolved