X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdatetime.php;h=ea98f01fe0999e63bc45400470f76c6fdb2c9a25;hb=b50769b6d09549e1641bab1813087fd928602129;hp=f75193b1bdb8cc8be85bb4bfda608c01ea337434;hpb=6348e70daa113e8b3203de8fbc919d08c90d972e;p=friendica.git diff --git a/include/datetime.php b/include/datetime.php index f75193b1bd..ea98f01fe0 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -1,23 +1,39 @@ '; usort($timezone_identifiers, 'timezone_cmp'); @@ -29,7 +45,7 @@ function select_timezone($current = 'America/Los_Angeles') { if($continent != '') $o .= ''; $continent = $ex[0]; - $o .= ""; + $o .= ''; } if(count($ex) > 2) $city = substr($value,strpos($value,'/')+1); @@ -38,108 +54,554 @@ function select_timezone($current = 'America/Los_Angeles') { } else { $city = $ex[0]; - if($continent != 'Miscellaneous') { + if($continent != t('Miscellaneous')) { $o .= ''; - $continent = 'Miscellaneous'; - $o .= ""; + $continent = t('Miscellaneous'); + $o .= ''; } } - $city = str_replace('_', ' ', $city); + $city = str_replace('_', ' ', t($city)); $selected = (($value == $current) ? " selected=\"selected\" " : ""); $o .= ""; - } + } $o .= ''; return $o; -}} +} + + +/** + * @brief Generating a Timezone selector + * + * Return a select using 'field_select_raw' template, with timezones + * groupped (primarily) by continent + * arguments follow convetion as other field_* template array: + * 'name', 'label', $value, 'help' + * + * @param string $name Name of the selector + * @param string $label Label for the selector + * @param string $current Timezone + * @param string $help Help text + * + * @return string Parsed HTML + */ +function field_timezone($name='timezone', $label='', $current = 'America/Los_Angeles', $help){ + $options = select_timezone($current); + $options = str_replace('','', $options); -if(! function_exists('datetime_convert')) { + $tpl = get_markup_template('field_select_raw.tpl'); + return replace_macros($tpl, array( + '$field' => array($name, $label, $current, $help, $options), + )); + +} + +/** + * @brief General purpose date parse/convert function. + * + * @param string $from Source timezone + * @param string $to Dest timezone + * @param string $s Some parseable date/time string + * @param string $fmt 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 + */ function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d H:i:s") { - $d = new DateTime($s, new DateTimeZone($from)); - $d->setTimeZone(new DateTimeZone($to)); - return($d->format($fmt)); -}} + // 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) == '0000-00-00') { + $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC')); + return str_replace('1','0',$d->format($fmt)); + } + + try { + $from_obj = new DateTimeZone($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($to); + } + catch(Exception $e) { + $to_obj = new DateTimeZone('UTC'); + } + + $d->setTimeZone($to_obj); + + return($d->format($fmt)); +} + + +/** + * @brief Wrapper for date selector, tailored for use in birthday fields. + * + * @param string $dob Date of Birth + * @return string Formatted html + */ +function dob($dob) { + list($year,$month,$day) = sscanf($dob,'%4d-%2d-%2d'); + $f = get_config('system','birthday_input_format'); + if(! $f) + $f = 'ymd'; + if($dob === '0000-00-00') + $value = ''; + else + $value = (($year) ? datetime_convert('UTC','UTC',$dob,'Y-m-d') : datetime_convert('UTC','UTC',$dob,'m-d')); -if(! function_exists('datesel')) { -function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) { + $age = ((intval($value)) ? age($value, $a->user["timezone"], $a->user["timezone"]) : ""); + + $o = replace_macros(get_markup_template("field_input.tpl"), array( + '$field' => array( + 'dob', + t('Birthday:'), + $value, + (((intval($age)) > 0 ) ? t('Age: ') . $age : ""), + '', + 'placeholder="' . t('YYYY-MM-DD or MM-DD') . '"' + ) + )); + +// if ($dob && $dob != '0000-00-00') +// $o = datesel($f,mktime(0,0,0,0,0,1900),mktime(),mktime(0,0,0,$month,$day,$year),'dob'); +// else +// $o = datesel($f,mktime(0,0,0,0,0,1900),mktime(),false,'dob'); + + return $o; +} + +/** + * @brief Returns a date selector + * + * @param string $format + * Format string, e.g. 'ymd' or 'mdy'. Not currently supported + * @param string $min + * Unix timestamp of minimum date + * @param string $max + * Unix timestap of maximum date + * @param string $default + * Unix timestamp of default date + * @param string $id + * ID and name of datetimepicker (defaults to "datetimepicker") + * + * @return string Parsed HTML output. + */ +function datesel($format, $min, $max, $default, $id = 'datepicker') { + return datetimesel($format,$min,$max,$default,'',$id,true,false, '',''); +} + +/** + * @brief Returns a time selector + * + * @param string $format + * Format string, e.g. 'ymd' or 'mdy'. Not currently supported + * @param $h + * Already selected hour + * @param $m + * Already selected minute + * @param string $id + * ID and name of datetimepicker (defaults to "timepicker") + * + * @return string Parsed HTML output. + */ +function timesel($format, $h, $m, $id='timepicker') { + return datetimesel($format,new DateTime(),new DateTime(),new DateTime("$h:$m"),'',$id,false,true); +} + +/** + * @brief Returns a datetime selector. + * + * @param string $format + * format string, e.g. 'ymd' or 'mdy'. Not currently supported + * @param string $min + * unix timestamp of minimum date + * @param string $max + * unix timestap 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 boolean $picktime + * true to show time picker (default) + * @param $minfrom + * set minimum date from picker with id $minfrom (none by default) + * @param $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. + */ +function datetimesel($format, $min, $max, $default, $label, $id = 'datetimepicker', $pickdate = true, $picktime = true, $minfrom = '', $maxfrom = '', $required = false) { + + // First day of the week (0 = Sunday) + $firstDay = get_pconfig(local_user(),'system','first_day_of_week'); + if ($firstDay === false) $firstDay=0; + + $lang = substr(get_browser_language(), 0, 2); + + // Check if the detected language is supported by the picker + if (!in_array($lang, array("ar", "ro", "id", "bg", "fa", "ru", "uk", "en", "el", "de", "nl", "tr", "fr", "es", "th", "pl", "pt", "ch", "se", "kr", "it", "da", "no", "ja", "vi", "sl", "cs", "hu"))) + $lang = ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en'); $o = ''; - $o .= "--"; return $o; -}} - - -// TODO rewrite this buggy sucker -function relative_date($posted_date) { - - $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); - - $in_seconds = strtotime($localtime); - - $diff = time() - $in_seconds; - - $months = floor($diff/2592000); - $diff -= $months*2419200; - $weeks = floor($diff/604800); - $diff -= $weeks*604800; - $days = floor($diff/86400); - $diff -= $days*86400; - $hours = floor($diff/3600); - $diff -= $hours*3600; - $minutes = floor($diff/60); - $diff -= $minutes*60; - $seconds = $diff; - - - if ($months>0) { - // over a month old, - return 'over a month ago'; - } else { - if ($weeks>0) { - // weeks and days - $relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks!=1 ?'s':''); - - } elseif ($days>0) { - // days and hours - $relative_date .= ($relative_date?', ':'').$days.' day'.($days!=1?'s':''); - - } elseif ($hours>0) { - // hours and minutes - $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours!=1?'s':''); - - } elseif ($minutes>0) { - // minutes only - $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes!=1?'s':''); - } else { - // seconds only - $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds!=1?'s':''); - } - } - // show relative date and add proper verbiage - return $relative_date.' ago'; +} + +/** + * @brief Create a birthday event. + * + * Update the year and the birthday. + */ +function update_contact_birthdays() { + + // This only handles foreign or alien networks where a birthday has been provided. + // In-network birthdays are handled within local_delivery + + $r = q("SELECT * FROM contact WHERE `bd` != '' AND `bd` != '0000-00-00' AND SUBSTRING(`bd`,1,4) != `bdyear` "); + if(count($r)) { + foreach($r as $rr) { + + logger('update_contact_birthday: ' . $rr['bd']); + + $nextbd = datetime_convert('UTC','UTC','now','Y') . substr($rr['bd'],4); + + /** + * + * Add new birthday event for this person + * + * $bdtext is just a readable placeholder in case the event is shared + * with others. We will replace it during presentation to our $importer + * to contain a sparkle link and perhaps a photo. + * + */ + + $bdtext = sprintf( t('%s\'s birthday'), $rr['name']); + $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $rr['url'] . ']' . $rr['name'] . '[/url]') ; + + $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`,`adjust`) + VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' ) ", + intval($rr['uid']), + intval($rr['id']), + dbesc(datetime_convert()), + dbesc(datetime_convert()), + dbesc(datetime_convert('UTC','UTC', $nextbd)), + dbesc(datetime_convert('UTC','UTC', $nextbd . ' + 1 day ')), + dbesc($bdtext), + dbesc($bdtext2), + dbesc('birthday'), + intval(0) + ); + + + // update bdyear + + q("UPDATE `contact` SET `bdyear` = '%s', `bd` = '%s' WHERE `uid` = %d AND `id` = %d", + dbesc(substr($nextbd,0,4)), + dbesc($nextbd), + intval($rr['uid']), + intval($rr['id']) + ); + + } + } }