+/**
+ * @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, $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 = '';
+ $dateformat = '';
+
+ if($pickdate) $dateformat .= 'Y-m-d';
+ if($pickdate && $picktime) $dateformat .= ' ';
+ if($picktime) $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 = $default ? 'value="' . date($dateformat, $default->getTimestamp()) . '"' : '';
+ $defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : '';
+
+ $pickers = '';
+ if(!$pickdate) $pickers .= ',datepicker: false';
+ if(!$picktime) $pickers .= ',timepicker: false';
+
+ $extra_js = '';
+ $pickers .= ",dayOfWeekStart: ".$firstDay.",lang:'".$lang."'";
+ if($minfrom != '')
+ $extra_js .= "\$('#$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#$id').data('xdsoft_datetimepicker').setOptions({minDate: currentDateTime})}})";
+ if($maxfrom != '')
+ $extra_js .= "\$('#$maxfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#$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);
+
+ $o .= "<div class='date'><input type='text' placeholder='$readable_format' name='$id' id='$id' $input_text />";
+ $o .= '</div>';
+ $o .= "<script type='text/javascript'>";
+ $o .= "\$(function () {var picker = \$('#$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})";
+ $o .= "</script>";
+
+ return $o;
+}
+
+/**
+ * @brief Returns a relative date string.
+ *
+ * Implements "3 seconds ago" etc.
+ * Based on $posted_date, (UTC).
+ * Results relative to current timezone.
+ * Limited to range of timestamps.
+ *
+ * @param string $posted_date
+ * @param string $format (optional) Parsed with sprintf()
+ * <tt>%1$d %2$s ago</tt>, e.g. 22 hours ago, 1 minute ago
+ *
+ * @return string with relative date
+ */
+function relative_date($posted_date,$format = null) {
+
+ $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date);
+
+ $abs = strtotime($localtime);
+
+ if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
+ return t('never');
+ }
+
+ $etime = time() - $abs;
+
+ if ($etime < 1) {
+ return t('less than a second ago');
+ }
+
+ /*
+ $time_append = '';
+ if ($etime >= 86400) {
+ $time_append = ' ('.$localtime.')';
+ }
+ */
+
+ $a = array( 12 * 30 * 24 * 60 * 60 => array( t('year'), t('years')),
+ 30 * 24 * 60 * 60 => array( t('month'), t('months')),
+ 7 * 24 * 60 * 60 => array( t('week'), t('weeks')),
+ 24 * 60 * 60 => array( t('day'), t('days')),
+ 60 * 60 => array( t('hour'), t('hours')),
+ 60 => array( t('minute'), t('minutes')),
+ 1 => array( t('second'), t('seconds'))
+ );
+
+ foreach ($a as $secs => $str) {
+ $d = $etime / $secs;
+ if ($d >= 1) {
+ $r = round($d);
+ // translators - e.g. 22 hours ago, 1 minute ago
+ if(! $format)
+ $format = t('%1$d %2$s ago');
+
+ return sprintf( $format,$r, (($r == 1) ? $str[0] : $str[1]));
+ }
+ }
+}
+
+/**
+ * @brief Returns timezone correct age in years.
+ *
+ * Returns the age in years, given a date of birth, the timezone of the person
+ * whose date of birth is provided, and the timezone of the person viewing the
+ * result.
+ *
+ * Why? Bear with me. Let's say I live in Mittagong, Australia, and my birthday
+ * is on New Year's. You live in San Bruno, California.
+ * When exactly are you going to see my age increase?
+ *
+ * A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start celebrating
+ * and become a year older. If you wish me happy birthday on January 1
+ * (San Bruno time), you'll be a day late.
+ *
+ * @param string $dob Date of Birth
+ * @param string $owner_tz (optional) Timezone of the person of interest
+ * @param string $viewer_tz (optional) Timezone of the person viewing
+ *
+ * @return int Age in years
+ */