1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
***********************************************************************************************
* Klasse erweitert das PHP-DateTime-Objekt um einige nuetzliche Funktionen
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class DateTimeExtended
*/
class DateTimeExtended extends DateTime
{
private $valid;
/**
* es muss das Datum und das dazugehoerige Format uebergeben werden
* @deprecated 3.2.0:4.0.0 Switched to native DateTime method. Use DateTime::createFromFormat()
* @param string $date String mit dem Datum
* @param string $format das zum Datum passende Format (Schreibweise aus date())
* @param \DateTimeZone $timezone DateTimeZone
*/
public function __construct($date, $format, DateTimeZone $timezone = null)
{
$datetime = DateTime::createFromFormat($format, $date);
if ($datetime === false)
{
$this->valid = false;
parent::__construct(null, $timezone);
}
else
{
$this->valid = true;
parent::__construct($datetime->format('Y-m-d H:i:s'), $timezone);
}
}
/**
* gibt true oder false zurueck, je nachdem ob DateTime gueltig ist
* @deprecated 3.2.0:4.0.0 Switched to native DateTime method. Use DateTime::createFromFormat() === false
* @return bool
*/
public function isValid()
{
return $this->valid;
}
/**
* berechnet aus dem Datum das Alter einer Person
* @deprecated 3.2.0:4.0.0 Switched to native DateTime method.
* Use DateTime::createFromFormat()->diff(new DateTime('now'))->y
* @return int
*/
public function getAge()
{
return $this->diff(new DateTime('now'))->y;
}
/**
* Returns an array with all 7 weekdays with full name in the specific language.
* @param int $weekday The number of the weekday for which the name should be returned (1 = Monday ...)
* @return string|string[] with all 7 weekday or if param weekday is set than the full name of that weekday
*/
public static function getWeekdays($weekday = 0)
{
global $gL10n;
$weekdays = array(
1 => $gL10n->get('SYS_MONDAY'),
2 => $gL10n->get('SYS_TUESDAY'),
3 => $gL10n->get('SYS_WEDNESDAY'),
4 => $gL10n->get('SYS_THURSDAY'),
5 => $gL10n->get('SYS_FRIDAY'),
6 => $gL10n->get('SYS_SATURDAY'),
7 => $gL10n->get('SYS_SUNDAY')
);
if ($weekday > 0)
{
return $weekdays[$weekday];
}
return $weekdays;
}
/**
* The method will convert a date format with the syntax of date()
* to a syntax that is known by the bootstrap datepicker plugin.
* e.g.: input: 'd.m.Y' output: 'dd.mm.yyyy'
* e.g.: input: 'j.n.y' output: 'd.m.yy'
* @param string $format Optional a format could be given in the date() syntax that should be transformed.
* If no format is set then the format of the class constructor will be used.
* @return string Return the transformed format that is valid for the datepicker.
*/
public static function getDateFormatForDatepicker($format = 'Y-m-d')
{
$destFormat = '';
$formatArray = str_split($format);
foreach ($formatArray as $formatChar)
{
switch($formatChar)
{
case 'd':
$destFormat .= 'dd';
break;
case 'j':
$destFormat .= 'd';
break;
case 'l':
$destFormat .= 'DD';
break;
case 'D':
$destFormat .= 'D';
break;
case 'm':
$destFormat .= 'mm';
break;
case 'n':
$destFormat .= 'm';
break;
case 'F':
$destFormat .= 'MM';
break;
case 'M':
$destFormat .= 'M';
break;
case 'Y':
$destFormat .= 'yyyy';
break;
case 'y':
$destFormat .= 'yy';
break;
default:
$destFormat .= $formatChar;
break;
}
}
return $destFormat;
}
}