QUESTION
This is following a previous question that was graciously answered by Milo earlier here – How to sort CPT by custom meta value (date), and return posts month by month
In short, I’m having some issues with properly formatting my custom meta box date’s in a few areas… Namely, in the back-end in my custom columns, and then in the front-end in my archive template.
For example, in my archive template I’m trying to return the current date that the posts belong to via the code pasted below, but when I visit a month with no posts my template returns the date “January 1970” instead.
<?php $calendar_month = get_post_meta($post->ID, 'epr_startdate', TRUE);?>
<?php $this_month = strtotime($calendar_month); ?>
<span id="current_month"><?php echo date( 'F Y', $this_month ); ?></span>
Additionally, in the back-end I’m trying to have it so that if just the “start date” is created then ONLY output that information, whereas, if the user enters both a “start date” and an “end date” then retun both dates, but when I use the code example below my start dates are returned in duplicate like this instead: Mar 3, 2013 â Mar 3, 2013.
case "eventdate":
$eventstart = get_post_meta($post->ID, 'epr_startdate', true);
$eventstart_col = strtotime($eventstart);
$eventend = get_post_meta($post->ID, 'epr_enddate', true);
$eventend_col = strtotime($eventend);
if ( get_post_meta($post->ID, 'epr_startdate', true) && ! get_post_meta($post->ID, 'epr_enddate', true) )
echo date( 'M n, Y', $eventstart_col );
elseif ( get_post_meta($post->ID, 'epr_enddate', true) )
echo date( 'M n, Y', $eventstart_col ) . ' — ' . date( 'M n, Y', $eventend_col );
else
echo 'NA';
break;
Thank you in advance for your time.
Best
FINAL SOLUTION
Big thank you to s_ha_dum for his patience and support!
Pasted below are the final working solutions to my question…
Correctly outputting the date when no posts exist for the currently queried month:
<?php $this_month = strtotime($calendar_month);
if (false === $this_month) {
$this_month = strtotime(get_query_var( 'calendar_year' ) . get_query_var( 'calendar_month' ) .'01');
} ?>
<span id="current_month"><?php echo date( 'F Y', $this_month ); ?></span><?php
?>
Custom columns:
case "eventdate":
$start_date = get_post_meta($post->ID, 'epr_startdate', true);
$start_date_col = strtotime($start_date);
$end_date = get_post_meta($post->ID, 'epr_enddate', true);
$end_date_col = strtotime($end_date);
if ( $start_date_col && !$end_date_col )
echo date( 'M d, Y', $start_date_col );
elseif ( $start_date_col && $end_date_col )
echo date( 'M d, Y', $start_date_col ) . ' — ' . date( 'M d, Y', $end_date_col );
else
echo 'NA';
break;
Redirecting /calendar -> /calendar/yyyy/mm:
function redirect_empty_archive() {
$m = get_query_var('calendar_month');
$y = get_query_var('calendar_year');
if (
is_post_type_archive('calendar') &&
( empty($m) || empty($y) )
) {
wp_safe_redirect( '/calendar' . date('/Y/m') );
}
}
add_action('template_redirect','redirect_empty_archive');
Yes. That will happen. UNIXTIME began on Jan 1, 1970. That is “0000/00/00” but negative numbers work back until sometime in 1901.
strtotime
will returnfalse
for anything outside that range, including your nonexistent dates.date
will assume “day zero” if given a bad date, hence you get Jan 1, 1970 for nonexistent or otherwise flawed dates. 64 bit machines can handle larger ranges, if I remember correctly. Either way a nonexistent date will give you 1970. Try:You want to make sure you have a good date before displaying it.
Your other code is a little bulky, but looks like it should work except that your date format is wrong. You aren’t getting “Month Day, Year” as I suspect you want but “Month-Name Month-Number, Year”, which will look like a duplicate if the two dates are in the same month/year. Take a good look at the date formatting operators.
I think I have that put back together correctly.