Something went wrong. Try again.
Date range field for Craft CMS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173<?php
namespace studioespresso\daterange\fields\data;
use Craft;use craft\base\FieldInterface;use craft\base\Serializable;use craft\helpers\DateTimeHelper;use craft\helpers\Json;use craft\i18n\Locale;use studioespresso\daterange\fields\DateRangeField;use yii\base\BaseObject;use yii\i18n\Formatter;
class DateRangeData extends BaseObject implements Serializable{
public $start;
public $end;
public $isFuture;
public $isOngoing;
public $isPast;
public $isNotPast;
public function __construct($value = null, $config = []) { $this->start = $value['start']; $this->end = $value['end'] ?: $value['start']; $this->isFuture = $this->getIsFuture(); $this->isOngoing = $this->getIsOngoing(); $this->isPast = $this->getIsPast(); $this->isNotPast = $this->getIsNotPast(); parent::__construct($config); }
public function serialize(): mixed { return [$this->start, $this->end]; }
public function getFormatted($format = 'd/m/Y', $seperator = ' - ', $locale = null) { if (is_array($format)) { if (isset($format['date'])) { $dateFormat = $format['date']; } if (isset($format['time'])) { $timeFormat = $format['time']; } $format = ($dateFormat ? $dateFormat : '') . ' ' . ($timeFormat ? $timeFormat : ''); } else { $format = $format; } $string = ''; $formatter = $locale ? (new Locale($locale))->getFormatter() : Craft::$app->getFormatter();
if($this->start->format('U') === $this->end->format('U')) { $string = $formatter->asDate($this->start, "php:$format"); } elseif ($this->start->format('dmy') === $this->end->format('dmy') && isset($timeFormat)) { $string .= $formatter->asDate($this->start, "php:$dateFormat"); $string .= " "; $string .= $formatter->asTime($this->start, "php:$timeFormat"); $string .= " " . trim($seperator) . " "; $string .= $formatter->asTime($this->end, "php:$timeFormat"); } else { $string .= $formatter->asDate($this->start, "php:$format"); $string .= " " . trim($seperator) . " "; $string .= $formatter->asDate($this->end, "php:$format"); } return $string; }
/** * @param \DateTime $start * @param \DateTime $end * @return bool * @throws \Exception */ public function getIsFuture() { $now = new \DateTime(); if ($this->start->format('U') > $now->format('U')) { return true; } return false; }
/** * @param \DateTime $start * @param \DateTime $end * @return bool * @throws \Exception */ public function getIsOngoing() {
$now = new \DateTime(); if ( $this->start->format('U') < $now->format('U') && $this->end->format('U') > $now->format('U') ) {
return true;
} return false; }
/** * @param \DateTime $start * @param \DateTime $end * @return bool * @throws \Exception */ public function getIsPast() { $now = new \DateTime(); if ($this->end->format('U') < $now->format('U')) { return true; } return false; }
/** * @param \DateTime $start * @param \DateTime $end * @return bool * @throws \Exception */ public function getIsNotPast() { $now = new \DateTime(); if ($this->end->format('U') > $now->format('U')) { return true; } return false; }
public static function normalize($value = null, FieldInterface $config) { if (!is_array($value)) { $value = Json::decode($value); }
if ((isset($value['start']['date']) && !$value['start']['date']) ) { return false; } else { if (isset($value['end']['date']) && !$value['end']['date']) { $value['end']['date'] = $value['start']['date']; } }
$start = $value['start']; $start = DateTimeHelper::toDateTime($start);
$end = $value['end']; $end = DateTimeHelper::toDateTime($end);
return [ 'start' => $start, 'end' => $end ]; }}