use DateTime;
use DateTimeZone;
-use Exception;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\PConfig;
*/
class Temporal
{
- const ATOM = 'Y-m-d\TH:i:s\Z';
- const MYSQL = 'Y-m-d H:i:s';
-
/**
* @brief Two-level sort for timezones.
*
$o = '<select id="timezone_select" name="timezone">';
- usort($timezone_identifiers, [self, 'timezoneCompareCallback']);
+ usort($timezone_identifiers, [__CLASS__, 'timezoneCompareCallback']);
$continent = '';
foreach ($timezone_identifiers as $value) {
$ex = explode("/", $value);
]);
}
- /**
- * convert() shorthand for UTC.
- *
- * @param string $time A date/time string
- * @param string $format DateTime format string or Temporal constant
- * @return string
- */
- public static function utc($time, $format = self::MYSQL)
- {
- return self::convert($time, 'UTC', 'UTC', $format);
- }
-
- /**
- * convert() shorthand for UTC now.
- *
- * @param string $format DateTime format string or Temporal constant
- * @return string
- */
- public static function utcNow($format = self::MYSQL)
- {
- return self::convert('now', 'UTC', 'UTC', $format);
- }
-
- /**
- * @brief General purpose date parse/convert/format function.
- *
- * @param string $s Some parseable date/time string
- * @param string $tz_to Destination timezone
- * @param string $tz_from Source timezone
- * @param string $format Output format recognised from php's DateTime class
- * http://www.php.net/manual/en/datetime.format.php
- *
- * @return string Formatted date according to given format
- */
- public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
- {
- // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
- // Provide some sane defaults regardless.
- if ($from === '') {
- $from = 'UTC';
- }
-
- if ($to === '') {
- $to = 'UTC';
- }
-
- if (($s === '') || (!is_string($s))) {
- $s = 'now';
- }
-
- /*
- * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
- * otherwise we end up with -0001-11-30 ...
- * add 32 days so that we at least get year 00, and then hack around the fact that
- * months and days always start with 1.
- */
- if (substr($s, 0, 10) <= '0001-01-01') {
- $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
- return str_replace('1', '0', $d->format($format));
- }
-
- try {
- $from_obj = new DateTimeZone($tz_from);
- } catch (Exception $e) {
- $from_obj = new DateTimeZone('UTC');
- }
-
- try {
- $d = new DateTime($s, $from_obj);
- } catch (Exception $e) {
- logger('datetime_convert: exception: ' . $e->getMessage());
- $d = new DateTime('now', $from_obj);
- }
-
- try {
- $to_obj = new DateTimeZone($tz_to);
- } catch (Exception $e) {
- $to_obj = new DateTimeZone('UTC');
- }
-
- $d->setTimeZone($to_obj);
-
- return $d->format($format);
- }
-
/**
* @brief Wrapper for date selector, tailored for use in birthday fields.
*
*/
public static function getDateofBirthField($dob)
{
+ $a = get_app();
+
list($year, $month, $day) = sscanf($dob, '%4d-%2d-%2d');
if ($dob < '0000-01-01') {
$value = '';
} else {
- $value = self::utc(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'Y-m-d');
+ $value = DateTimeFormat::utc(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'Y-m-d');
}
- $age = (intval($value) ? age($value, $a->user["timezone"], $a->user["timezone"]) : "");
+ $age = (intval($value) ? self::getAgeByTimezone($value, $a->user["timezone"], $a->user["timezone"]) : "");
$tpl = get_markup_template("field_input.tpl");
$o = replace_macros($tpl,
*/
public static function getDateField($min, $max, $default, $id = 'datepicker')
{
- return datetimesel($min, $max, $default, '', $id, true, false, '', '');
+ return self::getDateTimeField($min, $max, $default, '', $id, true, false, '', '');
}
/**
*/
public static function getTimeField($h, $m, $id = 'timepicker')
{
- return datetimesel(new DateTime(), new DateTime(), new DateTime("$h:$m"), '', $id, false, true);
+ return self::getDateTimeField(new DateTime(), new DateTime(), new DateTime("$h:$m"), '', $id, false, true);
}
/**
* @brief Returns a datetime selector.
*
- * @param string $min Unix timestamp of minimum date
- * @param string $max Unix timestamp of maximum date
- * @param string $default Unix timestamp of default date
- * @param string $id Id and name of datetimepicker (defaults to "datetimepicker")
- * @param bool $pickdate true to show date picker (default)
- * @param bool $picktime true to show time picker (default)
- * @param string $minfrom set minimum date from picker with id $minfrom (none by default)
- * @param string $maxfrom set maximum date from picker with id $maxfrom (none by default)
- * @param bool $required default false
+ * @param DateTime $minDate Minimum date
+ * @param DateTime $maxDate Maximum date
+ * @param DateTime $defaultDate Default date
+ * @param string $id Id and name of datetimepicker (defaults to "datetimepicker")
+ * @param bool $pickdate true to show date picker (default)
+ * @param bool $picktime true to show time picker (default)
+ * @param string $minfrom set minimum date from picker with id $minfrom (none by default)
+ * @param string $maxfrom set maximum date from picker with id $maxfrom (none by default)
+ * @param bool $required default false
*
* @return string Parsed HTML output.
*
* @todo Once browser support is better this could probably be replaced with
* native HTML5 date picker.
*/
- public static function getDateTimeField($min, $max, $default, $label, $id = 'datetimepicker', $pickdate = true,
- $picktime = true, $minfrom = '', $maxfrom = '', $required = false)
+ public static function getDateTimeField(
+ DateTime $minDate,
+ DateTime $maxDate,
+ DateTime $defaultDate,
+ $label,
+ $id = 'datetimepicker',
+ $pickdate = true,
+ $picktime = true,
+ $minfrom = '',
+ $maxfrom = '',
+ $required = false)
{
// First day of the week (0 = Sunday)
$firstDay = PConfig::get(local_user(), 'system', 'first_day_of_week', 0);
$dateformat .= 'H:i';
}
- $minjs = $min ? ",minDate: new Date({$min->getTimestamp()}*1000), yearStart: " . $min->format('Y') : '';
- $maxjs = $max ? ",maxDate: new Date({$max->getTimestamp()}*1000), yearEnd: " . $max->format('Y') : '';
+ $input_text = $defaultDate ? date($dateformat, $defaultDate->getTimestamp()) : '';
- $input_text = $default ? date($dateformat, $default->getTimestamp()) : '';
- $defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : '';
+ $readable_format = str_replace(['Y', 'm', 'd', 'H', 'i'], ['yyyy', 'mm', 'dd', 'HH', 'MM'], $dateformat);
- $pickers = '';
- if (!$pickdate) {
- $pickers .= ', datepicker: false';
- }
-
- if (!$picktime) {
- $pickers .= ',timepicker: false';
- }
-
- $extra_js = '';
- $pickers .= ",dayOfWeekStart: " . $firstDay . ",lang:'" . $lang . "'";
- if ($minfrom != '') {
- $extra_js .= "\$('#id_$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#id_$id').data('xdsoft_datetimepicker').setOptions({minDate: currentDateTime})}})";
- }
-
- if ($maxfrom != '') {
- $extra_js .= "\$('#id_$maxfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#id_$id').data('xdsoft_datetimepicker').setOptions({maxDate: currentDateTime})}})";
- }
-
- $readable_format = $dateformat;
- $readable_format = str_replace('Y', 'yyyy', $readable_format);
- $readable_format = str_replace('m', 'mm', $readable_format);
- $readable_format = str_replace('d', 'dd', $readable_format);
- $readable_format = str_replace('H', 'HH', $readable_format);
- $readable_format = str_replace('i', 'MM', $readable_format);
-
- $tpl = get_markup_template('field_input.tpl');
- $o .= replace_macros($tpl,
- [
+ $tpl = get_markup_template('field_datetime.tpl');
+ $o .= replace_macros($tpl, [
'$field' => [
$id,
$label,
$required ? '*' : '',
'placeholder="' . $readable_format . '"'
],
+ '$datetimepicker' => [
+ 'minDate' => $minDate,
+ 'maxDate' => $maxDate,
+ 'defaultDate' => $defaultDate,
+ 'dateformat' => $dateformat,
+ 'firstDay' => $firstDay,
+ 'lang' => $lang,
+ 'minfrom' => $minfrom,
+ 'maxfrom' => $maxfrom,
+ ]
]);
- $o .= "<script type='text/javascript'>";
- $o .= "\$(function () {var picker = \$('#id_$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})";
- $o .= "</script>";
-
return $o;
}
$viewer_tz = date_default_timezone_get();
}
- $birthdate = self::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d');
+ $birthdate = DateTimeFormat::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d');
list($year, $month, $day) = explode("-", $birthdate);
- $year_diff = self::convert('now', $viewer_tz, 'UTC', 'Y') - $year;
- $curr_month = self::convert('now', $viewer_tz, 'UTC', 'm');
- $curr_day = self::convert('now', $viewer_tz, 'UTC', 'd');
+ $year_diff = DateTimeFormat::timezoneNow($viewer_tz, 'Y') - $year;
+ $curr_month = DateTimeFormat::timezoneNow($viewer_tz, 'm');
+ $curr_day = DateTimeFormat::timezoneNow($viewer_tz, 'd');
if (($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day))) {
$year_diff--;
public static function getDaysInMonth($y, $m)
{
return date('t', mktime(0, 0, 0, $m, 1, $y));
- ;
}
/**
*
* @return string day 0 = Sunday through 6 = Saturday
*/
- public static function getFirstDayInMonth($y, $m)
+ private static function getFirstDayInMonth($y, $m)
{
$d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
- return self::utc($d, 'w');
+ return DateTimeFormat::utc($d, 'w');
}
/**
'October', 'November', 'December'
];
- $thisyear = self::convert('now', date_default_timezone_get(), 'UTC', 'Y');
- $thismonth = self::convert('now', date_default_timezone_get(), 'UTC', 'm');
+ $thisyear = DateTimeFormat::localNow('Y');
+ $thismonth = DateTimeFormat::localNow('m');
if (!$y) {
$y = $thisyear;
}
}
$dn = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
- $f = get_first_dim($y, $m);
- $l = get_dim($y, $m);
+ $f = self::getFirstDayInMonth($y, $m);
+ $l = self::getDaysInMonth($y, $m);
$d = 1;
$dow = 0;
$started = false;
if (($y == $thisyear) && ($m == $thismonth)) {
- $tddate = intval(self::convert('now', date_default_timezone_get(), 'UTC', 'j'));
+ $tddate = intval(DateTimeFormat::localNow('j'));
}
$str_month = day_translate($mtab[$m]);