Home Forums General Issues Query between dates using Date Picker fields Reply To: Query between dates using Date Picker fields

  • What I did was convert the stored data for my end date to unix timecode in my functions.php file like so

    function custom_unixtimesamp ( $post_id ) {
        if ( get_post_type( $post_id ) == 'events' ) {
    	$enddate = get_post_meta($post_id, 'event_date_ends', true);
    		if($enddate) {
    			$dateparts = explode('/', $enddate);
    			$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 queried as such

    $today = time();	
    	$args = array(
            'post_type' => 'events',
    	'post_status' => 'publish',
    	'posts_per_page' => '10',
    	'meta_query' => array(
    			'key' => 'unixstartdate',
    			'compare' => '>=',
    			'value' => $today,
    	'meta_key' => 'event_date_begins',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',

    However what I thought fixed the issue actually didn’t. While you’re trying for a monthly date range I’m looking for just upcoming events. I had 2 events created. One future date and one past date. Using this code the past date event went away so I thought problem solved until I went in to enter more future dates which did not show up. I removed the functions.php code and the result was the same so it must not be converting the time properly. I too am using yymmdd but sql, to my understanding need yy-mm-dd (2014-06-19).

    Are you using WordPress? If so how did you get it to understand the year without the hyphens?

    For your array couldn’t you just query event_start_date $current_month?

    'key' => 'event_start_date',
    'value' => $current_month

    then array

    'key' => 'event_end_date',
    'value' => $current_month

    using a compare of AND

    That should return anything with a start date for the month of current and end date for the current month.