but when I visit a month with no posts my template returns the date
“January 1970” instead.
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 return false
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:
$calendar_month = "1901/01/01";
$this_month = strtotime($calendar_month);
var_dump($this_month);
echo '<br />';
echo date('Y',$this_month);
echo '<br />';
You want to make sure you have a good date before displaying it.
<?php $this_month = strtotime($calendar_month);
if (false !== $this_month) { ?>
<span id="current_month"><?php echo date( 'F Y', $this_month ); ?></span><?php
}
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.
$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 ( $eventstart_col && !$eventend_col ) {
// only the start date
echo date( 'M d, Y', $eventstart_col );
} elseif ( $eventstart_col && $eventend_col ) {
// both start and end date
echo date( 'M d, Y', $eventstart_col ) . ' — ' . date( 'M d, Y', $eventend_col );
} else {
echo 'NA';
}
I think I have that put back together correctly.