/** * @param bool $withTime * * @return string */ public function calendar($withTime = true) { $momentNow = new Moment('now', $this->getTimezoneString()); $momentFromVo = $this->cloning()->startOf('day')->from($momentNow->startOf('day')); $diff = $momentFromVo->getDays(); // handle time string $renderedTimeString = MomentLocale::renderLocaleString(array('calendar', 'withTime'), array($this)); $addTime = false; // apply cases if ($diff > 7) { $localeKeys = array('calendar', 'default'); } elseif ($diff > 1) { $localeKeys = array('calendar', 'lastWeek'); $addTime = true; } elseif ($diff > 0) { $localeKeys = array('calendar', 'lastDay'); $addTime = true; } elseif ($diff == 0) { $localeKeys = array('calendar', 'sameDay'); $addTime = true; } elseif ($diff == -1) { $localeKeys = array('calendar', 'nextDay'); $addTime = true; } elseif ($diff > -7) { $localeKeys = array('calendar', 'sameElse'); $addTime = true; } else { $localeKeys = array('calendar', 'default'); } // render format $format = MomentLocale::renderLocaleString($localeKeys, array($this)); // add time if valid if ($addTime && $withTime === true) { $format .= ' ' . $renderedTimeString; } return $this->format($format); }
/** * @return string */ public function getRelative() { $formatArgs = array(); if ($this->valueInRange($this->getSeconds(), 0, 45)) { $localeKeys = array('relativeTime', 's'); $formatArgs[] = 1; } elseif ($this->valueInRange($this->getSeconds(), 45, 90)) { $localeKeys = array('relativeTime', 'm'); $formatArgs[] = 1; } elseif ($this->valueInRange($this->getSeconds(), 90, 45 * 60)) { $localeKeys = array('relativeTime', 'mm'); $formatArgs[] = $this->roundAbs($this->getMinutes()); } elseif ($this->valueInRange($this->getMinutes(), 45, 90)) { $localeKeys = array('relativeTime', 'h'); $formatArgs[] = 1; } elseif ($this->valueInRange($this->getMinutes(), 90, 22 * 60)) { $localeKeys = array('relativeTime', 'hh'); $formatArgs[] = $this->roundAbs($this->getHours()); } elseif ($this->valueInRange($this->getHours(), 22, 36)) { $localeKeys = array('relativeTime', 'd'); $formatArgs[] = 1; } elseif ($this->valueInRange($this->getHours(), 36, 25 * 24)) { $localeKeys = array('relativeTime', 'dd'); $formatArgs[] = $this->roundAbs($this->getDays()); } elseif ($this->valueInRange($this->getDays(), 25, 45)) { $localeKeys = array('relativeTime', 'M'); $formatArgs[] = 1; } elseif ($this->valueInRange($this->getDays(), 25, 345)) { $localeKeys = array('relativeTime', 'MM'); $formatArgs[] = $this->roundAbs($this->getMonths()); } elseif ($this->valueInRange($this->getDays(), 345, 547)) { $localeKeys = array('relativeTime', 'y'); $formatArgs[] = 1; } else { $localeKeys = array('relativeTime', 'yy'); $formatArgs[] = $this->roundAbs($this->getYears()); } // add to context $formatArgs[] = $this->getDirection(); $formatArgs[] = $this->getMoment(); // render value $time = MomentLocale::renderLocaleString($localeKeys, $formatArgs); // render value result by direction string return MomentLocale::renderLocaleString(array('relativeTime', $this->getDirection()), array($time)); }
/** * @return string */ public function getRelative() { $time = null; if ($this->valueInRange($this->getSeconds(), 0, 45)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 's')); } elseif ($this->valueInRange($this->getSeconds(), 45, 90)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'm')); } elseif ($this->valueInRange($this->getSeconds(), 90, 45 * 60)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'mm'), array(round(abs($this->getMinutes())))); } elseif ($this->valueInRange($this->getMinutes(), 45, 90)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'h')); } elseif ($this->valueInRange($this->getMinutes(), 90, 22 * 60)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'hh'), array(round(abs($this->getHours())))); } elseif ($this->valueInRange($this->getHours(), 22, 36)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'd')); } elseif ($this->valueInRange($this->getHours(), 36, 25 * 24)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'dd'), array(round(abs($this->getDays())))); } elseif ($this->valueInRange($this->getDays(), 25, 45)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'M')); } elseif ($this->valueInRange($this->getDays(), 25, 345)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'MM'), array(round(abs($this->getMonths())))); } elseif ($this->valueInRange($this->getDays(), 345, 547)) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'y')); } elseif ($this->getDays() > 547) { $time = MomentLocale::renderLocaleString(array('relativeTime', 'yy'), array(round(abs($this->getYears())))); } $baseString = MomentLocale::getLocaleString(array('relativeTime', $this->getDirection())); return vsprintf($baseString, array($time)); }