2

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 ) . ' &mdash; ' . 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 ) . ' &mdash; ' . 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');
6
  • To get an answer your going to have to make the question shorter, just show some simple examples of what you get compared to what you're expecting.
    – Wyck
    Mar 13, 2013 at 5:09
  • OK, thanks. I was worried too little would not be enough hence the long post. I will shorten it.
    – Mr.Brown
    Mar 13, 2013 at 5:36
  • I've truncated my question to only show the most important information now. Thanks.
    – Mr.Brown
    Mar 13, 2013 at 17:54
  • As regards the second part - are you sure you are saving the start and end dates correctly? You are not saving the start date into both fields?
    – vancoder
    Mar 13, 2013 at 18:19
  • I believe they are being saved correctly, yes. They are being stored via a date and time picker from the meta box plugin in yyyy/mm/dd format which is why I'm attempting to use strtotime to change the format on the front end. The user has the option to enter just a start date, or both.
    – Mr.Brown
    Mar 13, 2013 at 18:42

1 Answer 1

2

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 ) . ' &mdash; ' . date( 'M d, Y', $eventend_col );  
} else {
    echo 'NA';
}

I think I have that put back together correctly.

9
  • Thank you so much for the help! I'm out of town right now but when I get back in I will give this a go and report back. Really appreciate the response, thank you!
    – Mr.Brown
    Mar 16, 2013 at 23:43
  • The custom columns fix works excellent, thank you! The first part of your code only produced January 1901 for me however.
    – Mr.Brown
    Mar 17, 2013 at 4:25
  • The first part of my code would produce January 1901, as that is the date I hard-coded. That was illustrative. You aren't supposed to use that code for anything other than just proof of concept. If you are on a 32 bit machine that should return false. On a 64 bit it probably gives you the right date. I assume you are on a 64 bit machine. If you want to get false on a 64 bit machine just mess with the date. You will eventually get there but I don't know (without looking it up) exactly where that mark is. Use null or a very old date like "1000/01/01", or an empty string instead of a date.
    – s_ha_dum
    Mar 17, 2013 at 4:34
  • Doh! Sorry I missed that. Ok, well assuming Im doing this right, using an empty string returns nothing. Does it make a difference that this date is coming from a jquery date picker?
    – Mr.Brown
    Mar 17, 2013 at 5:22
  • 1
    let us continue this discussion in chat
    – s_ha_dum
    Mar 17, 2013 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.