certainlyakey
1/28/2015 - 8:40 AM

Wordpress - free entry date in metabox (with Metabox plugin)

Wordpress - free entry date in metabox (with Metabox plugin)

//allows user to input either correct 3 part dates in any format understandable by strtotime (such as 1999.03.01, 21.12.2013 etc) or any text (such as month or year only)
$works_date_meta = get_post_meta($post->ID,'lextypmb_works_date', true);
if ($works_date_meta) {
	//from: http://stackoverflow.com/questions/10691949/check-if-variable-is-a-valid-date-with-php
	$date_arr = date_parse($works_date_meta);
	if ($date_arr["error_count"] == 0 && checkdate($date_arr["month"], $date_arr["day"], $date_arr["year"])) {
		echo date('j F Y',strtotime($works_date_meta));
	}
	else {
		echo strtolower($works_date_meta);
	}
}

//needs Metabox add-a-metabox wrapper code
,array(
	 'name' => 'Defence date'
	,'id' => $prefix_cpt . 'date'
	,'type' => 'date'
	,'js_options' => array(
		'dateFormat' => 'dd.mm.yy'
		,'constrainInput' => false
		//,'altField' => '#'.$prefix_works.'date_sent' //use when ability to create loops with metadata dates as shown here http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters is needed 
		//,'altFormat' => 'yymmdd'
	)
	,'size' => '10'
)