setFormat() 공개 메소드

Set the date format.
public setFormat ( string $format )
$format string Date format. These are the valid options: http://php.net/manual/en/function.date.php
예제 #1
0
 public function testConstructor2()
 {
     $dt = new DateTimeObject('14.02.2013');
     $dt->setFormat('d.m.Y');
     $this->assertSame('14.02.2013', $dt->val());
 }
예제 #2
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;
 }