/** * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. * It takes into account the default date format for the current language if a LC_TIME file is used. * * @see \Cake\Utility\Time::i18nFormat() * * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object * @param string $format strftime format string. * @param bool|string $invalid Default value to display on invalid dates * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object * @return string Formatted and translated date string * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting * @throws \InvalidArgumentException When the date cannot be parsed */ public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) { try { $time = new Time($date, $timezone); return $time->i18nFormat($format, $timezone); } catch (\Exception $e) { if ($invalid === false) { throw $e; } return $invalid; } }
/** * Tests debugInfo * * @return void */ public function testDebugInfo() { $time = new Time('2014-04-20 10:10:10'); $expected = ['time' => '2014-04-20T10:10:10+0000', 'timezone' => 'UTC', 'fixedNowTime' => Time::getTestNow()->toISO8601String()]; $this->assertEquals($expected, $time->__debugInfo()); }
/** * Test that calling save, triggers an insert including the created and updated field values * * @return void */ public function testSaveTriggersInsert() { $this->loadFixtures('User'); $table = TableRegistry::get('users'); $table->addBehavior('Timestamp', ['events' => ['Model.beforeSave' => ['created' => 'new', 'updated' => 'always']]]); $entity = new Entity(['username' => 'timestamp test']); $return = $table->save($entity); $this->assertSame($entity, $return, 'The returned object is expected to be the same entity object'); $row = $table->find('all')->where(['id' => $entity->id])->first(); $now = Time::now(); $this->assertEquals($now, $row->created); $this->assertEquals($now, $row->updated); }
/** * Data provider for marshal() * * @return array */ public function marshalProvider() { $date = new Time('@1392387900'); $date->setTime(0, 0, 0); return [[null, null], [false, false], [true, true], ['', ''], ['derpy', 'derpy'], ['2013-nope!', '2013-nope!'], ['1392387900', $date], [1392387900, $date], ['2014-02-14', new Time('2014-02-14')], ['2014-02-14 13:14:15', new Time('2014-02-14 00:00:00')], [['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15], new Time('2014-02-14 00:00:00')], [['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 1, 'minute' => 14, 'second' => 15, 'meridian' => 'am'], new Time('2014-02-14 00:00:00')], [['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 1, 'minute' => 14, 'second' => 15, 'meridian' => 'pm'], new Time('2014-02-14 00:00:00')], [['year' => 2014, 'month' => 2, 'day' => 14], new Time('2014-02-14 00:00:00')], [['year' => 'farts', 'month' => 'derp'], new Time(date('Y-m-d 00:00:00'))], [['year' => 'farts', 'month' => 'derp', 'day' => 'farts'], new Time(date('Y-m-d 00:00:00'))], [['year' => '2014', 'month' => '02', 'day' => '14', 'hour' => 'farts', 'minute' => 'farts'], new Time('2014-02-14 00:00:00')]]; }