/** * Custom finder method, returns recent to-do's based on status * * @param Query $query cakephp query object * @param array $options list of options * @return query $query cakephp query object */ public function findRecent(Query $query, array $options = ['status' => 0]) { return $query->where(['is_done' => $options['status']])->order(['updated' => 'DESC'])->formatResults(function ($results, $query) { return $results->map(function ($row) { $timeCreated = new Time($row->created); $timeUpdated = new Time($row->updated); $row->created = $timeCreated->timeAgoInWords(); $row->updated = $timeUpdated->timeAgoInWords(); $row->todo = htmlspecialchars($row->todo); return $row; }); }); }
/** * test timeAgoInWords() with negative values. * * @return void */ public function testTimeAgoInWordsNegativeValues() { $time = new Time('-2 months -2 days'); $result = $time->timeAgoInWords(['end' => '3 month']); $this->assertEquals('2 months, 2 days ago', $result); $time = new Time('-2 months -2 days'); $result = $time->timeAgoInWords(['end' => '3 month']); $this->assertEquals('2 months, 2 days ago', $result); $time = new Time('-2 months -2 days'); $result = $time->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']); $this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result); $time = new Time('-2 years -5 months -2 days'); $result = $time->timeAgoInWords(['end' => '3 years']); $this->assertEquals('2 years, 5 months, 2 days ago', $result); $time = new Time('-2 weeks -2 days'); $result = $time->timeAgoInWords(['format' => 'yyyy-MM-dd']); $this->assertEquals('2 weeks, 2 days ago', $result); $time = new Time('-3 years -12 months'); $result = $time->timeAgoInWords(); $expected = 'on ' . $time->format('n/j/y'); $this->assertEquals($expected, $result); $time = new Time('-1 month -1 week -6 days'); $result = $time->timeAgoInWords(['end' => '1 year', 'accuracy' => ['month' => 'month']]); $this->assertEquals('1 month ago', $result); $time = new Time('-1 years -2 weeks -3 days'); $result = $time->timeAgoInWords(['accuracy' => ['year' => 'year']]); $expected = 'on ' . $time->format('n/j/y'); $this->assertEquals($expected, $result); $time = new Time('-13 months -5 days'); $result = $time->timeAgoInWords(['end' => '2 years']); $this->assertEquals('1 year, 1 month, 5 days ago', $result); $time = new Time('-58 minutes'); $result = $time->timeAgoInWords(['accuracy' => 'hour']); $this->assertEquals('about an hour ago', $result); $time = new Time('-23 hours'); $result = $time->timeAgoInWords(['accuracy' => 'day']); $this->assertEquals('about a day ago', $result); }
/** * Get pretty date like "Yesterday", "2 days ago", "etc" * * @return string */ public function getPrettyDate() { $time = new Time($this->_properties['updated_at']); return $time->timeAgoInWords(['format' => 'd']); }
/** * test the end option for timeAgoInWords * * @dataProvider timeAgoEndProvider * @return void */ public function testTimeAgoInWordsEnd($input, $expected, $end) { $time = new Time($input); $result = $time->timeAgoInWords(['end' => $end]); $this->assertEquals($expected, $result); }