If you want to compare dates in a MySQL statement, you should store dates and times using MySQL datetime format: YYYY-MM-DD HH:MM:SS (PHP format Y-m-d H:i:s
)
Then, you can compare dates easily, and 'type' => 'date'
in the meta query argument should work as expected:
$start_date = date('Y-m-d', strtotime($date." +15 days"));
$end_date = date('Y-m-d',strtotime($date." -15 days"));
$query_args['meta_query'][] = array(
'key' => '_start_date',
'value' => array($end_date, $start_date),
'compare' => 'BETWEEN',
'type' => 'date',
);
If you need to display the date and/or time in a different format, just convert it before display it.
For example, you can use date_i18n()
to display a localized date, based on WordPress language configuration, in the format you want. For exmple:
$post_id= 45;
$start_date = get_post_meta( $post_id. '_start_date', true );
// Format 15 October 2016 = d F Y
// See http://php.net/manual/en/function.date.php
echo date_i18n( 'd F Y', strtotime( $start_date ) );
Or, if you want to get the date format set in WordPress configuration:
echo date_i18n( get_option( 'date_format' ), strtotime( $start_date ) );