Something went wrong. Try again.
Date range field for Craft CMS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209<?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 DateTime;use DateTimeInterface;use studioespresso\daterange\DateRange;use yii\base\BaseObject;
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) { $dateFormat = "d/m/Y"; $timeFormat = "H:i:s";
if (is_array($format)) { if (isset($format['date'])) { $dateFormat = $format['date']; } if (isset($format['time'])) { $timeFormat = $format['time']; } $format = ( $dateFormat ) . ' ' . ( $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') && $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 function startsAfterDate(string|DateTimeInterface|int $date): bool { $date = DateTimeHelper::toDateTime($date);
return $this->start->format('U') > $date->format('U'); }
public function endsBeforeDate(string|DateTimeInterface|int $date): bool { $date = DateTimeHelper::toDateTime($date);
return $this->end->format('U') < $date->format('U'); }
public function isDuringDate(string|array|DateTimeInterface|int $date): bool { $dateRange = DateRange::toDateRange($date);
return ( $this->start->format('U') <= $dateRange['end']->format('U') && $this->end->format('U') >= $dateRange['start']->format('U') ); }
public function isNotDuringDate(string|array|DateTimeInterface|int $date): bool { $dateRange = DateRange::toDateRange($date);
return ( $this->start->format('U') > $dateRange['end']->format('U') || $this->end->format('U') < $dateRange['start']->format('U') ); }
public static function normalize($value, 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, ]; }}