Last active
December 19, 2015 08:38
-
-
Save ishtaka/5926579 to your computer and use it in GitHub Desktop.
[PHP]Dateマネージャー(DateTime)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dateマネージャー(DateTime) | |
* | |
*/ | |
class DateManager | |
{ | |
private $datetime = null; | |
private $dateInterval = null; | |
/** | |
* コンストラクタ | |
* @access public | |
* @param string $date :日付 | |
* @param string $timezone :タイムゾーン | |
* @throws LogicException :ErrorMessage | |
*/ | |
public function __construct($date, $timezone = 'Asia/Tokyo') | |
{ | |
try { | |
$this->datetime = new DateTime($date, new DateTimeZone($timezone)); | |
} catch (Exception $e) { | |
throw new LogicException($e->getMessage()); | |
} | |
} | |
/** | |
* タイムゾーン設定 | |
* @access public | |
* @param string $timezone :タイムゾーン | |
* @throws LogicException :ErrorMessage | |
*/ | |
public function setTimeZone($timezone) | |
{ | |
if(!$this->datetime->setTimeZone(new DateTimeZone($timezone))) { | |
throw new LogicException('Failed to set TimeZone'); | |
} | |
} | |
/** | |
* インターバル設定 | |
* @access public | |
* @param string $interval :インターバル | |
* @throws LogicException :ErrorMessage | |
*/ | |
public function setInterval($interval) | |
{ | |
try { | |
$this->dateInterval = new DateInterval($interval); | |
} catch (Exception $e) { | |
throw new LogicException($e->getMessage()); | |
} | |
} | |
/** | |
* DateTimeオブジェクトを返す | |
* @access public | |
* @return Object $datetime | |
*/ | |
public function getObj() | |
{ | |
return $this->datetime; | |
} | |
/** | |
* 日付を返す | |
* @access public | |
* @param string $format :日付フォーマット | |
* @return string $date :日付 | |
*/ | |
public function getDate($format) | |
{ | |
if (!$date = $this->datetime->format($format)) { | |
throw new LogicException('Failed to format'); | |
} | |
return $date; | |
} | |
/** | |
* 日付加算 | |
* @access public | |
* @param string $format :日付フォーマット | |
* @throws LogicException :ErrorMessage | |
* @return string $date :日付 | |
*/ | |
public function addDate($format) | |
{ | |
if (!is_object($this->dateInterval)) { | |
throw new LogicException('Inaterval is not set'); | |
} | |
$datetime = clone $this->datetime; | |
$date = $datetime->add($this->dateInterval)->format($format); | |
if (!$date) { | |
throw new LogicException('Failed to add Date'); | |
} | |
return $date; | |
} | |
/** | |
* 日付減算 | |
* @access public | |
* @param string $format :日付フォーマット | |
* @throws LogicException :ErrorMessage | |
* @return string $date :日付 | |
*/ | |
public function subDate($format) | |
{ | |
if (!is_object($this->dateInterval)) { | |
throw new LogicException('Inaterval is not set'); | |
} | |
$datetime = clone $this->datetime; | |
$date = $datetime->sub($this->dateInterval)->format($format); | |
if (!$date) { | |
throw new LogicException('Failed to sub Date'); | |
} | |
return $date; | |
} | |
/** | |
* 日付差分 | |
* @access public | |
* @param string $date :日付 | |
* @param string $format :日付フォーマット | |
* @param bool $abs :absolute default = false | |
* @throws LogicException :ErrorMessage | |
* @return string result :差分 default 総日数 | |
*/ | |
public function diffDate($date, $format = '%a', $abs = false) | |
{ | |
try { | |
$diffObj = new DateTime($date); | |
} catch (Exception $e) { | |
throw new LogicException($e->getMessage()); | |
} | |
$diff = $this->datetime->diff($diffObj); | |
return $diff->format($format); | |
} | |
/** | |
* 繰り返し演算 | |
* @access public | |
* @param string $date :日付 | |
* @param string $format :日付フォーマット | |
* @return Array $dateArray :日付配列 | |
*/ | |
public function period($date, $format) | |
{ | |
if (!is_object($this->dateInterval)) { | |
throw new LogicException('Inaterval is not set'); | |
} | |
$date = $date . ' +1 sec'; | |
$period = new DatePeriod( | |
$this->datetime, | |
$this->dateInterval, | |
new DateTime($date) | |
); | |
foreach ($period as $day) { | |
$dateArray[] = $day->format($format); | |
} | |
return $dateArray; | |
} | |
/** | |
* 繰り返し演算(回数) | |
* @access public | |
* @param int $repeat :繰り返し回数 | |
* @param string $format :日付フォーマット | |
* @return Array $dateArray :日付配列 | |
*/ | |
public function repeat($repeat, $format) | |
{ | |
if (!is_object($this->dateInterval)) { | |
throw new LogicException('Inaterval is not set'); | |
} | |
$period = new DatePeriod( | |
$this->datetime, | |
$this->dateInterval, | |
$repeat | |
); | |
foreach ($period as $day) { | |
$dateArray[] = $day->format($format); | |
} | |
return $dateArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment