How to modify this query so start/end dates go by days instead of exact times?

I’m using the query below to get posts based on the start and end dates which use the UNIX timestamp.

Currently, it’s only returning results if the start time occurred after today’s time. For example, if it’s 5:00 PM and the start date is 6:00 PM, it won’t pick it up. I would like it to consider anytime during today (from midnight last night to midnight tonight) as being the start date.

Read More

Can someone tell me how to update my code to get this to work?

$current_time is equal to 1305132894, which is 5:54 EST today.

'meta_query'        => array(
    array(
        'key'       => 'start_date',
        'value'     => $current_time,
        'compare'   => '<'
    ),
    array(
        'key'       => 'end_date',
        'value'     => $current_time,
        'compare'   => '>'
    )
)

Related posts

Leave a Reply

2 comments

  1. If you only need it to work for today, you can use:

    $minDate = strtotime('midnight today');
    $maxDate = strtotime('midnight tomorrow -1 second');
    

    or alternatively

    $minDate = mktime(0,0,0);
    $maxDate = mktime(11,59,59);
    

    Some combination of mktime() and strtotime() should work.

  2. Considering your example: $current_time = 1305132894

    This should answer your question:

    $date_current = date("Y-m-d",1305132894);  //2011-05-11
    $date_midnight = strtotime($date_current . "00:00:00"); //1305061200 that means: 2011-05-11 00:00:00
    

    So replace $current time by $date_midnight and you’ll have anytime during today from midnight last night.