Created
April 10, 2022 20:02
-
-
Save jules0x/d9d1fb4cf576315efdf84660cb7e7daa to your computer and use it in GitHub Desktop.
Date range display value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Determine the best way to render the date based on conditions | |
* | |
* @return string | null | |
*/ | |
public function getEventDate() | |
{ | |
$start = DBDate::create()->setValue($this->owner->StartDate); | |
$end = DBDate::create()->setValue($this->owner->EndDate); | |
// Nothing to display if no start date | |
if (!$start) { | |
return null; | |
} | |
// If no end date OR hide end date, just display start date | |
if (!$end) { | |
return $start->Format('d MMM y'); | |
} | |
$d1 = $start->Format('d'); | |
$d2 = $end->Format('d'); | |
$m1 = $start->Format('MMM'); | |
$m2 = $end->Format('MMM'); | |
$y1 = $start->Format('y'); | |
$y2 = $end->Format('y'); | |
$str = ''; | |
if (!$d1 || !$m2 || !$y2) { | |
return "$d1 $m1 $y1"; | |
} elseif ($y1 != $y2) { | |
return "$d1 $m1 $y1 – $d2 $m2 $y2"; | |
} elseif ($m1 != $m2) { | |
$str = "$d1 $m1 – $d2 $m2 $y2"; | |
} elseif ($d1 != $d2) { | |
$str = "$d1 – $d2 $m1 $y2"; | |
} else { | |
$str = "$d1 $m1 $y2"; | |
} | |
return $str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment