/** * getVar get variable prepared according to format * * @param XoopsObject $obj object containing variable * @param string $key name of variable * @param string $format Dtype::FORMAT_* constant indicating desired formatting * * @return int|DateTime */ public function getVar(XoopsObject $obj, $key, $format) { $storedValue = $obj->vars[$key]['value']; switch (strtolower($format)) { case Dtype::FORMAT_NONE: case 'n': $value = $storedValue; break; default: $value = Time::cleanTime($storedValue); break; } return $value; }
/** * __construct * * @param string|array $caption caption or array of all attributes * @param string $name name * @param integer|\DateTime $value unix timestamp or DateTime object */ public function __construct($caption, $name = null, $value = null) { if (is_array($caption)) { parent::__construct($caption); $this->setIfNotSet('size', 15); $this->setIfNotSet('value', 0); $this->set('value', \Xoops\Core\Locale\Time::cleanTime($this->get('value', null))); } else { parent::__construct([]); $this->setCaption($caption); $this->setName($name); $this->set('size', 15); $this->setValue(\Xoops\Core\Locale\Time::cleanTime($value)); } }
/** * __construct * * @param string|array $caption Caption or array of all attributes * @param string $name name * @param integer|\DateTime $value unix timestamp or DateTime object */ public function __construct($caption, $name = null, $value = 0) { // stash everything in the tray and sort out later if (is_array($caption)) { parent::__construct($caption); $this->set(':joiner', ''); } else { parent::__construct($caption, '', $name); $this->set('value', $value); } $workingTime = \Xoops\Core\Locale\Time::cleanTime($this->get('value', 0)); $dateDefinition = ['caption' => '', 'name' => $this->get('name') . '[date]', 'id' => $this->get('name') . '-date', 'size' => 15, 'value' => $workingTime, ElementFactory::FORM_KEY => $this]; new DateSelect($dateDefinition); $minuteInterval = $this->get(':minuteinterval', 15); $hours = (int) ltrim($workingTime->format('H'), '0'); $minutes = (int) ltrim($workingTime->format('i'), '0'); $timeDefinition = ['caption' => '', 'name' => $this->get('name') . '[time]', 'id' => $this->get('name') . '-time', 'size' => 1, 'value' => \Xoops\Core\Locale\Time::formatTime($hours * 3600 + 60 * $minuteInterval * ceil($minutes / $minuteInterval), 'short', new \DateTimeZone('UTC')), ElementFactory::FORM_KEY => $this, 'option' => \Xoops\Core\Lists\Time::getList($minuteInterval)]; new Select($timeDefinition); }
/** * @dataProvider formatTimestampProvider */ public function test_formatTimestamp($locale, $timezone, $format, $shortform, $expected) { $instance = $this->myClass; \Xoops\Locale::setTimeZone(new \DateTimeZone($timezone)); \Xoops\Locale::setCurrent($locale); $dateTime = \Xoops\Core\Locale\Time::cleanTime(); $dateTime->setDate(2015, 12, 14); $dateTime->setTime(0, 0, 0); $time = $dateTime->getTimestamp(); $value = $instance::formatTimestamp($time, $format); $this->assertSame($expected, $value); if (!empty($shortform)) { $value = $instance::formatTimestamp($time, $shortform); $this->assertSame($expected, $value); } }
/** * Get User Timestamp (kind of pointless, since timestamps are UTC?) * * @param \DateTime|int $time DateTime object or unix timestamp * * @return int unix timestamp */ public function getUserTimestamp($time) { $dt = \Xoops\Core\Locale\Time::cleanTime($time); return $dt->getTimestamp(); }
/** * Function to display formatted times in user timezone * * @param mixed $time * @param string $format Format codes () * 's' or 'short' - short; * 'm' or 'medium' - medium; * 'l' or 'long' - long; * 'c' or 'custom' - format determined according to interval to present; * 'e' or 'elapse' - Elapsed; * 'mysql' - Y-m-d H:i:s; * 'rss' * * @return string */ public static function formatTimestamp($time, $format = 'l') { $workingTime = Time::cleanTime($time); switch (strtolower($format)) { case 'short': case 's': return Time::formatDateTime($workingTime, 'short'); case 'medium': case 'm': return Time::formatDateTime($workingTime, 'medium'); case 'long': case 'l': return Time::formatDateTime($workingTime, 'long'); case 'full': case 'f': return Time::formatDateTime($workingTime, 'full'); case 'custom': case 'c': $specialName = Calendar::getDateRelativeName($workingTime, true); if ($specialName != '') { return $specialName; } // no break - fall through // no break - fall through case 'elapse': case 'e': return Time::describeRelativeInterval($workingTime); case 'short-date': return Time::formatDate($workingTime, 'short'); case 'short-time': return Time::formatTime($workingTime, 'short'); case 'medium-date': return Time::formatDate($workingTime, 'medium'); case 'medium-time': return Time::formatTime($workingTime, 'medium'); case 'long-date': return Time::formatDate($workingTime, 'long'); case 'long-time': return Time::formatTime($workingTime, 'long'); case 'full-date': return Time::formatDate($workingTime, 'full'); case 'full-time': return Time::formatTime($workingTime, 'full'); case 'rss': $workingTime->setTimezone(new \DateTimeZone('UTC')); return $workingTime->format($workingTime::RSS); case 'mysql': $workingTime->setTimezone(new \DateTimeZone('UTC')); return $workingTime->format('Y-m-d H:i:s'); default: if ($format != '') { return $workingTime->format($format); } return Time::formatDateTime($workingTime, 'long'); break; } }
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ use Xoops\Core\Request; use Xoops\Core\Locale\Time; /** * @copyright 2011-2015 XOOPS Project (http://xoops.org) * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) * @author trabis <*****@*****.**> * @author Richard Griffith <*****@*****.**> */ include dirname(dirname(__DIR__)) . '/mainfile.php'; $xoops = Xoops::getInstance(); $xoops->header(); $default = Time::cleanTime(); $dateOnly = Request::getDateTime('date', $default); $dateAndTime = Request::getDateTime('date_time', $default); // Date demo form $form = new Xoops\Form\ThemeForm('Date and Time', 'form_localedates', '', 'post'); $date = new Xoops\Form\DateSelect('Date', 'date', $dateOnly); $date->setDescription(\XoopsLocale::formatTimestamp($dateOnly, 'custom')); $form->addElement($date, true); $date_time = new Xoops\Form\DateTime('Date time', 'date_time', $dateAndTime); $date_time->setDescription(Time::describeRelativeInterval($dateAndTime)); $form->addElement($date_time, true); $buttonSubmit = new Xoops\Form\Button('', 'submit', XoopsLocale::A_SUBMIT, 'submit'); $form->addElement($buttonSubmit); $form->display(); // Locale selection form $localePicker = new Xoops\Form\ThemeForm('Change Locale', 'form_locale', '', 'get');
/** * Return a DateTime object from a Xoops\Form\DateSelect of Xoops\Form\DateTime field * * @param string $name Variable name * @param mixed $default Default value if the variable does not exist * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) * * @return \DateTime object */ public static function getDateTime($name, $default = null, $hash = 'default') { $values = self::getVar($name, [], $hash, 'array'); $count = count($values); if ($count === 1) { $date = reset($values); $ret = Time::inputToDateTime($date); } elseif (isset($values['date']) && isset($values['time'])) { $ret = Time::inputToDateTime($values); } else { $ret = Time::cleanTime($default); } return $ret; }