@ 2014-02-25T10:49:44Z <?php
/**
* Wrapper for PHP's DateTime class inspired by moment.js
*
* @author Tino Ehrich <ehrich@efides.com>
* @version See composer.json
*
* @dependencies >= PHP 5.3.0
*/
namespace Moment;
class Moment extends \DateTime
{
/** @var string */
protected $_timezoneString;
// ##########################################
/**
* @param string $dateTime
* @param string $timezone
*/
public function __construct($dateTime = 'now', $timezone = 'UTC')
{
return $this->resetDateTime($dateTime, $timezone);
}
// ##########################################
/**
* @param string $timezoneString
*
* @return Moment
*/
protected function _setTimezoneString($timezoneString)
{
$this->_timezoneString = $timezoneString;
return $this;
}
// ##########################################
/**
* @return string
*/
protected function _getTimezoneString()
{
return $this->_timezoneString;
}
// ##########################################
/**
* @param string $timezone
*
* @return \DateTimeZone
*/
protected function _getDateTimeZone($timezone)
{
return new \DateTimeZone($timezone);
}
// ##########################################
/**
* @param string $timezone
*
* @return \DateTime|Moment
*/
public function setTimezone($timezone)
{
$this->_setTimezoneString($timezone);
parent::setTimezone($this->_getDateTimeZone($timezone));
return $this;
}
// ##########################################
/**
* @param string $dateTime
* @param string $timezone
*
* @return Moment
*/
public function resetDateTime($dateTime = 'now', $timezone = 'UTC')
{
// cache timezone string
$this->_setTimezoneString($timezone);
// create instance
parent::__construct($dateTime, $this->_getDateTimeZone($timezone));
return $this;
}
// ##########################################
/**
* @param null $format
* @param null|FormatsInterface $formatsInterface
*
* @return string
*/
public function format($format = NULL, $formatsInterface = NULL)
{
// set default format
if ($format === NULL)
{
$format = \DateTime::ISO8601;
}
// handle diverse format types
if ($formatsInterface instanceof FormatsInterface)
{
$format = $formatsInterface->format($format);
}
return parent::format($format);
}
// ############################################
/**
* @param string $type
* @param int $value
*
* @return Moment
*/
public function add($type = 'day', $value = 1)
{
parent::modify('+' . $value . ' ' . $type);
return $this;
}
// ############################################
/**
* @param string $type
* @param int $value
*
* @return Moment
*/
public function subtract($type = 'day', $value = 1)
{
parent::modify('-' . $value . ' ' . $type);
return $this;
}
// ######################################
/**
* @param string $dateTime
* @param string $timezone
*
* @return MomentFromVo
*/
public function from($dateTime = 'now', $timezone = 'UTC')
{
$fromInstance = parent::diff(new Moment($dateTime, $timezone));
return (new MomentFromVo())
->setDirection($fromInstance->format('%R'))
->setSeconds($this->_fromToSeconds($fromInstance))
->setMinutes($this->_fromToMinutes($fromInstance))
->setHours($this->_fromToHours($fromInstance))
->setDays($this->_fromToDays($fromInstance))
->setWeeks($this->_fromToWeeks($fromInstance));
}
// ######################################
/**
* @param string $timezone
*
* @return MomentFromVo
*/
public function fromNow($timezone = 'UTC')
{
return $this->from('now', $timezone);
}
// ######################################
/**
* @param \DateInterval $dateInterval
*
* @return string
*/
protected function _fromToSeconds(\DateInterval $dateInterval)
{
return ($dateInterval->y * 365 * 24 * 60 * 60) + ($dateInterval->m * 30 * 24 * 60 * 60) + ($dateInterval->d * 24 * 60 * 60) + ($dateInterval->h * 60 * 60) + $dateInterval->s;
}
// ######################################
/**
* @param \DateInterval $dateInterval
*
* @return string
*/
protected function _fromToMinutes(\DateInterval $dateInterval)
{
return $this->_fromToSeconds($dateInterval) / 60;
}
// ######################################
/**
* @param \DateInterval $dateInterval
*
* @return string
*/
protected function _fromToHours(\DateInterval $dateInterval)
{
return $this->_fromToMinutes($dateInterval) / 60;
}
// ######################################
/**
* @param \DateInterval $dateInterval
*
* @return string
*/
protected function _fromToDays(\DateInterval $dateInterval)
{
return $this->_fromToHours($dateInterval) / 24;
}
// ######################################
/**
* @param \DateInterval $dateInterval
*
* @return string
*/
protected function _fromToWeeks(\DateInterval $dateInterval)
{
return $this->_fromToDays($dateInterval) / 7;
}
// ######################################
/**
* @param $period
*
* @return MomentPeriodVo
* @throws \Exception
*/
public function getPeriod($period)
{
switch ($period)
{
case 'week':
$currentWeekDay = $this->format('N');
$start = (new Moment('@' . $this->format('U')))
->setTimezone($this->_getTimezoneString())
->subtract('day', $currentWeekDay - 1)
->setTime(0, 0, 0);
$end = (new Moment('@' . $this->format('U')))
->setTimezone($this->_getTimezoneString())
->add('day', 7 - $currentWeekDay)
->setTime(23, 59, 59);
break;
// ------------------------------
case 'month':
$maxMonthDays = $this->format('t');
$currentMonthDay = $this->format('j');
$start = (new Moment('@' . $this->format('U')))
->setTimezone($this->_getTimezoneString())
->subtract('day', $currentMonthDay - 1)
->setTime(0, 0, 0);
$end = (new Moment('@' . $this->format('U')))
->setTimezone($this->_getTimezoneString())
->add('day', $maxMonthDays - $currentMonthDay)
->setTime(23, 59, 59);
break;
// ------------------------------
default:
throw new \Exception("Period \"{$period}\" is not supported yet (supported are \"week\" and \"month\").", 500);
}
return (new MomentPeriodVo())
->setRefDate($this)
->setStartDate($start)
->setEndDate($end);
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Deprecated: Return type of Moment\Moment::format($format = null, $formatsInterface = null) should either be compatible with DateTime::format(string $format): string, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/rQ0K4 on line 109
Deprecated: Return type of Moment\Moment::add($type = 'day', $value = 1) should either be compatible with DateTime::add(DateInterval $interval): DateTime, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/rQ0K4 on line 134
Deprecated: Return type of Moment\Moment::setTimezone($timezone) should either be compatible with DateTime::setTimezone(DateTimeZone $timezone): DateTime, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/rQ0K4 on line 73
Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 Output for 5.3.0 - 5.3.29 Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/rQ0K4 on line 169
Process exited with code 255 . preferences:dark mode live preview
220.28 ms | 401 KiB | 318 Q