Generating HTML

When generating HTML you should keep in mind that the relative time difference generated by Date_HumanDiff is only valid for the very moment it got created. A couple of seconds later it may be wrong - if it was "one minute ago", it needs to be "two minutes ago" then.

The first step to cater for that problem is to provide the original date and time value together with the relative difference. The Microformats project has a matching convention for this task, the datetime-design-pattern, and HTML5 has a semantically equivalent element: <time>.

Both methods use RFC 3339 style date and time values, for example 1996-12-19T16:39:57-08:00.

Expressing the original timestamp using a microformat

<?php
require_once 'Date/HumanDiff.php';
$dh = new Date_HumanDiff();

//fictive creation date of e.g. a blog post
$creationDate 1346277600;
$relDiff $dh->get($creationDate);//age of blog post
?>
<abbrev title="<?php echo date('c'$creationDate); ?>">
 <?php echo htmlspecialchars($relDiff); ?>
</abbrev>

Expressing the original timestamp with the HTML5 time tag

<?php
require_once 'Date/HumanDiff.php';
$dh = new Date_HumanDiff();

//fictive creation date of e.g. a blog post
$creationDate 1346277600;
$relDiff $dh->get($creationDate);//age of blog post
?>
<time datetime="<?php echo date('c'$creationDate); ?>">
 <?php echo htmlspecialchars($relDiff); ?>
</time>
Using translations (Previous) Encryption (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.