]> git.mxchange.org Git - friendica.git/blobdiff - include/datetime.php
Merge pull request #3333 from Rudloff/feature/theme-color
[friendica.git] / include / datetime.php
index e27f54e09cd529015c924dec7ce3634e0127654f..287e9a2d84a232bf666b54d38f66ab55c3c5cb1b 100644 (file)
@@ -4,6 +4,7 @@
  * @brief Some functions for date and time related tasks.
  */
 
+use \Friendica\Core\Config;
 
 /**
  * @brief Two-level sort for timezones.
@@ -125,7 +126,7 @@ function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d
        // 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') {
+       if(substr($s,0,10) <= '0001-01-01') {
                $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
                return str_replace('1','0',$d->format($fmt));
        }
@@ -170,7 +171,7 @@ function dob($dob) {
        $f = get_config('system','birthday_input_format');
        if(! $f)
                $f = 'ymd';
-       if($dob === '0000-00-00')
+       if($dob <= '0001-01-01')
                $value = '';
        else
                $value = (($year) ? datetime_convert('UTC','UTC',$dob,'Y-m-d') : datetime_convert('UTC','UTC',$dob,'m-d'));
@@ -188,7 +189,7 @@ function dob($dob) {
                )
        ));
 
-//     if ($dob && $dob != '0000-00-00')
+//     if ($dob && $dob > '0001-01-01')
 //             $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');
@@ -213,7 +214,7 @@ function dob($dob) {
  * @return string Parsed HTML output.
  */
 function datesel($format, $min, $max, $default, $id = 'datepicker') {
-       return datetimesel($format,$min,$max,$default,$id,true,false, '','');
+       return datetimesel($format,$min,$max,$default,'',$id,true,false, '','');
 }
 
 /**
@@ -231,7 +232,7 @@ function datesel($format, $min, $max, $default, $id = 'datepicker') {
  * @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);
+       return datetimesel($format,new DateTime(),new DateTime(),new DateTime("$h:$m"),'',$id,false,true);
 }
 
 /**
@@ -262,7 +263,7 @@ function timesel($format, $h, $m, $id='timepicker') {
  * @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) {
+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');
@@ -271,8 +272,9 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
        $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');
+       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 = Config::get('system', 'language', 'en');
+       }
 
        $o = '';
        $dateformat = '';
@@ -284,7 +286,7 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
        $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()) . '"' : '';
+       $input_text = $default ? date($dateformat, $default->getTimestamp()) : '';
        $defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : '';
 
        $pickers = '';
@@ -294,9 +296,9 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
        $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})}})";
+               $extra_js .= "\$('#id_$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#id_$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})}})";
+               $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);
@@ -305,10 +307,13 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
        $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>';
+       $tpl = get_markup_template('field_input.tpl');
+       $o .= replace_macros($tpl,array(
+                       '$field' => array($id, $label, $input_text, '', (($required) ? '*' : ''), 'placeholder="' . $readable_format . '"'),
+               ));
+
        $o .= "<script type='text/javascript'>";
-       $o .= "\$(function () {var picker = \$('#$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})";
+       $o .= "\$(function () {var picker = \$('#id_$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})";
        $o .= "</script>";
 
        return $o;
@@ -322,19 +327,19 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
  * Results relative to current timezone.
  * Limited to range of timestamps.
  *
- * @param string $posted_date
+ * @param string $posted_date MySQL-formatted date string (YYYY-MM-DD HH:MM:SS)
  * @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) {
+function relative_date($posted_date, $format = null) {
 
-       $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date);
+       $localtime = $posted_date . ' UTC';
 
        $abs = strtotime($localtime);
 
-       if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
+       if (is_null($posted_date) || $posted_date <= NULL_DATE || $abs === False) {
                 return t('never');
        }
 
@@ -344,13 +349,6 @@ function relative_date($posted_date,$format = null) {
                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')),
@@ -365,10 +363,11 @@ function relative_date($posted_date,$format = null) {
                if ($d >= 1) {
                        $r = round($d);
                        // translators - e.g. 22 hours ago, 1 minute ago
-                       if(! $format)
+                       if (!$format) {
                                $format = t('%1$d %2$s ago');
+                       }
 
-                       return sprintf( $format,$r, (($r == 1) ? $str[0] : $str[1]));
+                       return sprintf($format, $r, (($r == 1) ? $str[0] : $str[1]));
                }
        }
 }
@@ -554,9 +553,9 @@ 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) {
+       $r = q("SELECT * FROM contact WHERE `bd` != '' AND `bd` > '0001-01-01' AND SUBSTRING(`bd`,1,4) != `bdyear` ");
+       if (dbm::is_result($r)) {
+               foreach ($r as $rr) {
 
                        logger('update_contact_birthday: ' . $rr['bd']);
 
@@ -572,6 +571,17 @@ function update_contact_birthdays() {
                         *
                         */
 
+                       // Check for duplicates
+                       $s = q("SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
+                               intval($rr['uid']),
+                               intval($rr['id']),
+                               dbesc(datetime_convert('UTC','UTC', $nextbd)),
+                               dbesc('birthday'));
+
+                       if (dbm::is_result($s)) {
+                               continue;
+                       }
+
                        $bdtext = sprintf( t('%s\'s birthday'), $rr['name']);
                        $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $rr['url'] . ']' . $rr['name'] . '[/url]') ;