Class that enables you to work with dates and time much easier.
상속: extends Webiny\Component\StdLib\StdObject\AbstractStdObject, use trait ValidatorTrait, use trait ManipulatorTrait
예제 #1
0
 /**
  * Populates the cache control headers with options so that the browser caches the content.
  *
  * @param DateTimeObject $expirationDate Defines the date when the cache should expire.
  *
  * @return $this
  */
 public function setAsCache(DateTimeObject $expirationDate)
 {
     $expirationDateFormatted = date('D, d M Y H:i:s', $expirationDate->getTimestamp());
     $this->cacheControl->key('Expires', $expirationDateFormatted . ' GMT');
     $maxAge = $expirationDate->getTimestamp() - time();
     $this->cacheControl->key('Cache-Control', 'private, max-age=' . $maxAge);
     $this->cacheControl->key('Last-Modified', date('D, d M Y H:i:s') . ' GMT');
     return $this;
 }
예제 #2
0
 public function testSetAsCache()
 {
     $cc = new CacheControl();
     $dt = new DateTimeObject();
     $cc->setAsCache($dt->add('3 hours'));
     $ccHeaders = $cc->getCacheControl();
     $maxAge = $dt->getTimestamp() - time();
     $this->assertSame('private, max-age=' . $maxAge, $ccHeaders['Cache-Control']);
     $dtFormatted = date('D, d M Y H:i:s', $dt->getTimestamp());
     $this->assertSame($dtFormatted . ' GMT', $ccHeaders['Expires']);
 }
예제 #3
0
 /**
  * Returns the last modified time
  *
  * @param string $key
  *
  * @param bool   $asDateTimeObject (Optional) Return as DateTimeObject if true
  *
  * @return UNIX Timestamp or DateTimeObject
  */
 public function getTimeModified($key, $asDateTimeObject = false)
 {
     $time = $this->driver->getTimeModified($key);
     if ($asDateTimeObject) {
         $datetime = new DateTimeObject();
         return $datetime->setTimestamp($time);
     }
     return $time;
 }
예제 #4
0
 public function testSmallerThan2()
 {
     $dt = new DateTimeObject('14.02.2013');
     $this->assertFalse($dt->smallerThan('13.02.2013'));
 }
예제 #5
0
 /**
  * Create a DateTimeObject from the given $time and $format.
  *
  * @param string|int  $time Timestamp.
  * @param null|string $format Format in which the current timestamp is defined.
  *
  * @return DateTimeObject
  * @throws DateTimeObjectException
  */
 public static function createFromFormat($time, $format = null)
 {
     if (self::isNull($format)) {
         $format = self::$defaultFormat;
     }
     try {
         $date = \DateTime::createFromFormat($format, $time);
         if (!$date) {
             throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
         }
     } catch (\Exception $e) {
         throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
     }
     try {
         $dt = new DateTimeObject();
         $dt->setTimestamp($date->getTimestamp());
         $dt->setFormat($format);
     } catch (\Exception $e) {
         throw new DateTimeObjectException(DateTimeObjectException::MSG_UNABLE_TO_CREATE_FROM_FORMAT);
     }
     return $dt;
 }