datetime

On recent weekends, I’ve been playing around with my blog. I’m not an engineer, so working on my site is a time when I get to try my hand at basic coding. One thing that most of you have probably noticed is that my blog was returning the incorrect value for the datetime attribute on posts. I apologize for any inconvenience that this has caused my readers. Specifically, my site was displaying the PDT time as if it were UTC. On my previous post, you would see something like this:

<time class="entry-date" datetime="2014-10-31T22:01:56+00:00">October 31, 2014 10:01 pm</time>

Weird, huh? Doesn’t that indicate that it was posted at 3 PM in California? After reading how to format the date and time, I ended up manually creating what I think ‘c’ should return by doing this in my functions.php file:

esc_attr( get_the_date( 'Y-m-d\TH:i:sT' ) )

That seemed to do the trick. Now that same post looks like this:

<time class="entry-date" datetime="2014-10-31T22:01:56PDT">October 31, 2014 at 10:01 pm</time>

For comprehensiveness, here’s what I currently have. It might be helpful for someone else or for me to refer to later. I’ve certainly benefited from others sharing their code.  And yeah, I know that I’m calling stuff that I’m not using but I didn’t want to change the numbers around:

// Set up and print post meta information.
 printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%7$s">%3$s at %6$s</time></a></span> <span class="byline"><span class="author vcard">By <a class="url fn n" href="/about/" rel="author">%5$s</a></span></span>',
 esc_url( get_permalink() ),
 esc_attr( get_the_date( 'c' ) ),
 esc_html( get_the_date() ),
 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 get_the_author(),
 esc_html( get_the_time() ),
 esc_attr( get_the_date( 'Y-m-d\TH:i:sT' ) ) // Did this because using c as per the template default was returning the local time without correctly specifying the offset or timezone, so I did the formatting manually after reading http://codex.wordpress.org/Formatting_Date_and_Time
 );

Let me know in the comments if I was misinterpreting the old behavior as a bug.

Update on February 22nd, 2015: The Twenty Fifteen theme uses a function called twentyfifteen_entry_meta. To get this to work, copy that function from /wp-content/themes/twentyfifteen/inc/template-tags.php over to functions.php in your child theme. Then replace any parameter set to ‘c’ to ‘Y-m-d\TH:i:sT’ instead.

Leave a Reply

Your email address will not be published. Required fields are marked *