Using HTML5 time element in WordPress themes
I've begun retrofitting this site with HTML5 elements. I'm thoroughly enjoying the process (I love meaningful markup).
One of the first HTML5 elements I've introduced is the time element which, through its datetime attribute, provides a machine-readable version of dates and times.
<time datetime="2009-11-01T16:41:53+13:00">1 November 2009</time>
I wrote a function to generate the machine-readable dates and times for blog comments.
/**
* echoes comment's date and time in format 2009-11-01T03:41:53+13:00
*/
function comment_datetime()
{
$comment = get_comment($comment);
$local = strtotime($comment->comment_date);
$gmt = strtotime($comment->comment_date_gmt);
$seconds = abs($local - $gmt);
$hours = (int) ($seconds / 3600);
$minutes = (int) (($seconds - $hours * 3600) / 60);
$output = get_comment_time('Y-m-d\TH:i:s');
if ($local == $gmt)
$output .= 'Z';
else
$output .= ($local > $gmt ? '+' : '-')
. str_pad($hours, 2, '0', STR_PAD_LEFT) . ':'
. str_pad($minutes, 2, '0', STR_PAD_LEFT);
echo $output;
}
While looping through comments in your WordPress theme, call the above function to print a valid datetime string.
<time datetime="<?php comment_datetime(); ?>">
Comments
lol no need to do all that stuff!
just use the comment_date function with the string parameter 'Y-m-dTH:i:sO', PHP date format rulez :P
Excellent suggestion, Hikari! In fact, there's an even simpler syntax:
<time datetime="<?php comment_date('c'); ?>">
(Note that the format character c, representing ISO 8601, was added in PHP 5.)
Unfortunately, 'c' (and 'Y-m-dTH:i:sP') fails to handle the time zone offset. I would love to do away with the comment_datetime function, but at this stage I am not aware of a workable alternative. If you know of one please let me know!
hmm what your function does that Y-m-dTH:i:sO?
For comments I'm using this full code, it is microformat compatible as long as I know
<abbr class="published" title="">
Just change abbr for date and title for datetime since you are using HTML5. For posts it is the same:
<abbr class="published" title="">
I didn't understand why you are comparing comment_date with comment_date_gmt, maybe you don't believe your server's timezone config, or want to support a config change? I don't know where exactally Wordpress gets its data from...
Here's my understanding of how WordPress attaches times to comments:
- WordPress gets UTC from server
- WordPress calculates local time by applying specified offset
- WordPress saves both times with comment
Ideally, WordPress would include the offset in the output of comment_date('c'). Since it does not appear to do so, one must compare the two saved values. I'd love to discover that I'm wrong, as using a built-in function would be much nicer.
I understand... Well about when a comments is done IDK, but when it is loaded what it uses is mysql2date($d, $comment->comment_date);, so it has mysql and PHP involved.
I still don't understand why Wordpress stores both GMT and timezone times, since date is stored as a long int with miliseconds precision, it should just store GMT and apply timezone over it.
Have you compared these 3 ways of using comment_date to see if they show different times over the same timezone? If that's enough important to you, make a post about it with the results :)