public function testTruncate() { $rt = new \RelativeTime\RelativeTime(); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours, 12 minutes, 3 seconds ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 10)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours, 12 minutes, 3 seconds ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 5)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours, 12 minutes ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 4)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 3)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 2)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 1)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => 0)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours, 12 minutes, 3 seconds ago'); $rt = new \RelativeTime\RelativeTime(array('truncate' => -1)); $result = $rt->convert('2013-03-25 07:35:02', '2010-08-25 16:22:59'); $this->assertEquals($result, '2 years, 6 months, 30 days, 15 hours, 12 minutes, 3 seconds ago'); }
<?php require 'vendor/autoload.php'; // If you want to delete old comments, make this true. We use it to clean up the demo. $deleteOldComments = false; // Setting up the data store $dir = __DIR__ . '/data'; $config = new \JamesMoss\Flywheel\Config($dir, array('formatter' => new \JamesMoss\Flywheel\Formatter\JSON())); $repo = new \JamesMoss\Flywheel\Repository('shouts', $config); // Delete comments which are more than 1 hour old if the variable is set to be true. if ($deleteOldComments) { $oldShouts = $repo->query()->where('createdAt', '<', strtotime('-1 hour'))->execute(); foreach ($oldShouts as $old) { $repo->delete($old->id); } } // Send the 20 latest shouts as json $shouts = $repo->query()->orderBy('createdAt ASC')->limit(20, 0)->execute(); $results = array(); $config = array('language' => '\\RelativeTime\\Languages\\English', 'separator' => ', ', 'suffix' => true, 'truncate' => 1); $relativeTime = new \RelativeTime\RelativeTime($config); foreach ($shouts as $shout) { $shout->timeAgo = $relativeTime->timeAgo($shout->createdAt); $results[] = $shout; } header('Content-type: application/json'); echo json_encode($results);
function relative_time($date) { static $rel; if (!isset($rel)) { $config = array('language' => '\\RelativeTime\\Languages\\English', 'separator' => ', ', 'suffix' => true, 'truncate' => 1); $rel = new \RelativeTime\RelativeTime($config); } return $rel->timeAgo($date); }