/** * {@inheritdoc} * * @uses Titon\Utility\Time */ public function to($value) { if (is_array($value)) { $hour = isset($value['hour']) ? $value['hour'] : 0; if (isset($value['meridiem']) && strtolower($value['meridiem']) === 'pm') { $hour += 12; } $timestamp = mktime($hour, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0, isset($value['month']) ? $value['month'] : date('m'), isset($value['day']) ? $value['day'] : date('d'), isset($value['year']) ? $value['year'] : date('Y')); $value = Time::factory(date('Y-m-d H:i:s', $timestamp), isset($value['timezone']) ? $value['timezone'] : null); } if ($value instanceof DateTime) { return $value->format($this->format); } return date($this->format, Time::toUnix($value)); }
/** * Convert the expires date into a valid UNIX timestamp, * or if $ttl is true it will convert to relative seconds. * * @param mixed $timestamp * @param bool $ttl Convert to TTL seconds * @return int */ public function expires($timestamp, $ttl = false) { if ($timestamp === 0) { return $timestamp; } else { if ($timestamp === null) { $timestamp = strtotime($this->getConfig('expires')); } else { if (is_string($timestamp)) { $timestamp = Time::toUnix($timestamp); if ($ttl) { $timestamp -= time(); } } } } return $timestamp; }
/** * Validate input is a real date. * * @uses Titon\Utility\Time * * @param string $input * @return bool */ public static function date($input) { $input = Time::toUnix($input); if (!$input) { return false; } list($m, $d, $y) = explode('/', date('m/d/Y', $input)); return checkdate($m, $d, $y); }
/** * Purge all soft deleted records from the database. * If a time frame is provided, delete all records within that time frame. * If no time frame is provided, delete all records based on flag or timestamp being not null. * * @uses \Titon\Utility\Time * * @param int|string $timeFrame * @return int */ public function purgeDeleted($timeFrame) { $config = $this->allConfig(); return $this->getRepository()->deleteMany(function (Query $query) use($timeFrame, $config) { if ($timeFrame) { $query->where($config['deleteField'], '>=', Time::toUnix($timeFrame)); } else { if ($config['useFlag']) { $query->where($config['flagField'], true); } else { $query->where($config['deleteField'], '!=', null); } } }, ['before' => false, 'after' => false]); }
/** * Test that toUnix() returns any time of argument as a unix timestamp. */ public function testToUnix() { $this->assertTrue(is_numeric(Time::toUnix(null))); $this->assertTrue(is_numeric(Time::toUnix(time()))); $this->assertTrue(is_numeric(Time::toUnix('+1 week'))); $this->assertTrue(is_numeric(Time::toUnix(new DateTime()))); $this->assertFalse(is_numeric(Time::toUnix('string'))); }
/** * Format a time string. * * @uses Titon\Utility\Time * * @param string|int $time * @param string $format * @return string */ public static function time($time, $format = '%H:%M:%S') { return strftime($format, Time::toUnix($time)); }