Ejemplo n.º 1
0
 /**
  * {@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));
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
  * 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);
 }
Ejemplo n.º 4
0
 /**
  * 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]);
 }
Ejemplo n.º 5
0
 /**
  * Test that wasWithinLast() returns true if the passed date was within the last time span.
  */
 public function testWasWithinLast()
 {
     $this->assertTrue(Time::wasWithinLast('-1 day', '-7 days'));
     $this->assertTrue(Time::wasWithinLast('-37 hours', '-7 days'));
     $this->assertTrue(Time::wasWithinLast('-1 week', '-7 days'));
     $this->assertFalse(Time::wasWithinLast('+1 day', '-7 days'));
     $this->assertFalse(Time::wasWithinLast('-8 days', '-7 days'));
 }
Ejemplo n.º 6
0
 /**
  * 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));
 }