diff options
Diffstat (limited to 'include/datetime.php')
-rw-r--r-- | include/datetime.php | 111 |
1 files changed, 53 insertions, 58 deletions
diff --git a/include/datetime.php b/include/datetime.php index fe0f29c7a..346d03bd4 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -14,25 +14,17 @@ function timezone_cmp($a, $b) { return ( t($a) < t($b)) ? -1 : 1; } -// emit a timezone selector grouped (primarily) by continent - -function select_timezone($current = 'America/Los_Angeles') { - +// Return timezones grouped (primarily) by continent +function get_timezones( ){ $timezone_identifiers = DateTimeZone::listIdentifiers(); - - $o ='<select id="timezone_select" name="timezone">'; usort($timezone_identifiers, 'timezone_cmp'); $continent = ''; + $continents = array(); foreach($timezone_identifiers as $value) { $ex = explode("/", $value); if(count($ex) > 1) { - if($ex[0] != $continent) { - if($continent != '') - $o .= '</optgroup>'; - $continent = $ex[0]; - $o .= '<optgroup label="' . t($continent) . '">'; - } + $continent = t($ex[0]); if(count($ex) > 2) $city = substr($value,strpos($value,'/')+1); else @@ -40,35 +32,14 @@ function select_timezone($current = 'America/Los_Angeles') { } else { $city = $ex[0]; - if($continent != t('Miscellaneous')) { - $o .= '</optgroup>'; - $continent = t('Miscellaneous'); - $o .= '<optgroup label="' . t($continent) . '">'; - } + $continent = t('Miscellaneous'); } $city = str_replace('_', ' ', t($city)); - $selected = (($value == $current) ? " selected=\"selected\" " : ""); - $o .= "<option value=\"$value\" $selected >$city</option>"; - } - $o .= '</optgroup></select>'; - return $o; -} - -// 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' -function field_timezone($name='timezone', $label='', $current = 'America/Los_Angeles', $help){ - $options = select_timezone($current); - $options = str_replace('<select id="timezone_select" name="timezone">','', $options); - $options = str_replace('</select>','', $options); - - $tpl = get_markup_template('field_select_raw.tpl'); - return replace_macros($tpl, array( - '$field' => array($name, $label, $current, $help, $options), - )); - + if(!x($continents,$ex[0])) $continents[$ex[0]] = array(); + $continents[$continent][$value] = $city; + } + return $continents; } // General purpose date parse/convert function. @@ -134,7 +105,17 @@ function dob($dob) { if(! $f) $f = 'ymd'; - $o = datesel($f,mktime(0,0,0,0,0,1900),mktime(),mktime(0,0,0,$month,$day,$year),'dob'); + if($dob === '0000-00-00') + $value = ''; + else + $value = (($year) ? datetime_convert('UTC','UTC',$dob,'Y-m-d') : datetime_convert('UTC','UTC',$dob,'m-d')); + + $o = '<input type="text" name="dob" value="' . $value . '" 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; } @@ -153,12 +134,12 @@ function dob($dob) { * @param $id * id and name of datetimepicker (defaults to "datetimepicker") */ -function datesel($format, $min, $max, $default,$id = 'datepicker') { - return datetimesel($format,$min,$max,$default,$id,true,false); +function datesel($format, $min, $max, $default, $id = 'datepicker') { + return datetimesel($format,$min,$max,$default,$id,true,false, '',''); } /** - * returns a date selector + * returns a time selector * @param $format * format string, e.g. 'ymd' or 'mdy'. Not currently supported * @param $h @@ -168,8 +149,8 @@ function datesel($format, $min, $max, $default,$id = 'datepicker') { * @param $id * id and name of datetimepicker (defaults to "timepicker") */ -function timesel($format,$h,$m,$id='timepicker') { - return datetimesel($format,mktime(),mktime(),mktime($h,$m),$id,false,true); +function timesel($format, $h, $m, $id='timepicker') { + return datetimesel($format,new DateTime(),new DateTime(),new DateTime("$h:$m"),$id,false,true); } /** @@ -194,31 +175,42 @@ function timesel($format,$h,$m,$id='timepicker') { * set maximum date from picker with id $maxfrom (none by default) */ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pickdate = true, $picktime = true, $minfrom = '', $maxfrom = '') { + // Once browser support is better this could probably be replaced with native HTML5 date picker $o = ''; $dateformat = ''; - if($pickdate) $dateformat .= 'YYYY-MM-DD'; + + if($pickdate) $dateformat .= 'Y-m-d'; if($pickdate && $picktime) $dateformat .= ' '; - if($picktime) $dateformat .= 'HH:mm'; + if($picktime) $dateformat .= 'H:i'; - $mindate = $min ? "new Date($min*1000)" : ''; - $maxdate = $max ? "new Date($max*1000)" : ''; + $minjs = $min ? ",minDate: new Date({$min->getTimestamp()}*1000), yearStart: " . $min->format('Y') : ''; + $maxjs = $max ? ",maxDate: new Date({$max->getTimestamp()}*1000), yearEnd: " . $max->format('Y') : ''; - $defaultDate = $default ? ", defaultDate: new Date($default*1000)" : ''; + $input_text = $default ? 'value="' . date($dateformat, $default->getTimestamp()) . '"' : ''; + $defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : ''; $pickers = ''; - if(!$pickdate) $pickers .= 'pickDate: false,'; - if(!$picktime) $pickers .= 'pickTime: false,'; + if(!$pickdate) $pickers .= ',datepicker: false'; + if(!$picktime) $pickers .= ',timepicker: false'; $extra_js = ''; if($minfrom != '') - $extra_js .= "\$('#$minfrom').on('dp.change',function (e) { \$('#$id').data('DateTimePicker').setMinDate(e.date); });"; + $extra_js .= "\$('#$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#$id').data('xdsoft_datetimepicker').setOptions({minDate: currentDateTime})}})"; if($maxfrom != '') - $extra_js .= "\$('#$maxfrom').on('dp.change',function (e) { \$('#$id').data('DateTimePicker').setMaxDate(e.date); });"; - - $o .= "<div class='date' id='$id'><input type='text' placeholder='$dateformat' name='$id'/></div>"; - $o .= "<script type='text/javascript'>\$(function () {\$('#$id').datetimepicker({sideBySide: true, $pickers minDate: $mindate, maxDate: $maxdate, format: '$dateformat', useCurrent: false $defaultDate}); $extra_js});</script>"; + $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'>\$(function () {var picker = \$('#$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})</script>"; return $o; } @@ -450,7 +442,10 @@ function update_birthdays() { require_once('include/permissions.php'); $r = q("SELECT * FROM abook left join xchan on abook_xchan = xchan_hash - WHERE abook_dob > utc_timestamp() + interval 7 day and abook_dob < utc_timestamp() + interval 14 day"); + WHERE abook_dob > %s + interval %s and abook_dob < %s + interval %s", + db_utcnow(), db_quoteinterval('7 day'), + db_utcnow(), db_quoteinterval('14 day') + ); if($r) { foreach($r as $rr) { @@ -472,7 +467,7 @@ function update_birthdays() { $z = event_store_event($ev); if($z) { $item_id = event_store_item($ev,$z); - q("update abook set abook_dob = '%s' where abook_id = %d limit 1", + q("update abook set abook_dob = '%s' where abook_id = %d", dbesc(intval($rr['abook_dob']) + 1 . substr($rr['abook_dob'],4)), intval($rr['abook_id']) ); |