From b20bc1406d0886ceab3bfa2b5a1d7b9f4844ec01 Mon Sep 17 00:00:00 2001 From: Max Pozdeev Date: Wed, 21 Oct 2009 21:50:46 +0400 Subject: [PATCH] + configurable date format --- src/ajax.php | 49 +++++++++++++++++++++++++++++++------- src/common.php | 3 +++ src/db/config.php.default | 5 ++++ src/index.php | 3 ++- src/lang/class.default.php | 14 ----------- src/lang/en.php | 2 -- 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/ajax.php b/src/ajax.php index 7955dc4..ff929a4 100644 --- a/src/ajax.php +++ b/src/ajax.php @@ -486,12 +486,18 @@ function tag_size($qmin, $q, $step) function parse_duedate($s) { + global $config; $y = $m = $d = 0; if(preg_match("|^(\d+)-(\d+)-(\d+)\b|", $s, $ma)) { $y = (int)$ma[1]; $m = (int)$ma[2]; $d = (int)$ma[3]; } - elseif(preg_match("|^(\d+)\/(\d+)\/(\d+)\b|", $s, $ma)) { - $m = (int)$ma[1]; $d = (int)$ma[2]; $y = (int)$ma[3]; + elseif(preg_match("|^(\d+)\/(\d+)\/(\d+)\b|", $s, $ma)) + { + if($config['duedateformat'] == 4) { + $d = (int)$ma[1]; $m = (int)$ma[2]; $y = (int)$ma[3]; + } else { + $m = (int)$ma[1]; $d = (int)$ma[2]; $y = (int)$ma[3]; + } } elseif(preg_match("|^(\d+)\.(\d+)\.(\d+)\b|", $s, $ma)) { $d = (int)$ma[1]; $m = (int)$ma[2]; $y = (int)$ma[3]; @@ -502,8 +508,13 @@ function parse_duedate($s) if( $m<(int)$a[1] || ($m==(int)$a[1] && $d<(int)$a[2]) ) $y = (int)$a[0]+1; else $y = (int)$a[0]; } - elseif(preg_match("|^(\d+)\/(\d+)\b|", $s, $ma)) { - $m = (int)$ma[1]; $d = (int)$ma[2]; + elseif(preg_match("|^(\d+)\/(\d+)\b|", $s, $ma)) + { + if($config['duedateformat'] == 4) { + $d = (int)$ma[1]; $m = (int)$ma[2]; + } else { + $m = (int)$ma[1]; $d = (int)$ma[2]; + } $a = explode(',', date('Y,m,d')); if( $m<(int)$a[1] || ($m==(int)$a[1] && $d<(int)$a[2]) ) $y = (int)$a[0]+1; else $y = (int)$a[0]; @@ -537,18 +548,19 @@ function prepare_duedate($duedate, $tz=null) } $diff = mktime(0,0,0,$ad[1],$ad[2],$ad[0]) - mktime(0,0,0,$at[1],$at[2],$at[0]); - if($diff < -604800 && $ad[0] == $at[0]) { $a['class'] = 'past'; $a['str'] = $lang->formatMD((int)$ad[1], (int)$ad[2]); } - elseif($diff < -604800) { $a['class'] = 'past'; $a['str'] = $lang->formatYMD((int)$ad[0], (int)$ad[1], (int)$ad[2]); } + if($diff < -604800 && $ad[0] == $at[0]) { $a['class'] = 'past'; $a['str'] = formatDate3($config['dateformatshort'], (int)$ad[0], (int)$ad[1], (int)$ad[2]); } + elseif($diff < -604800) { $a['class'] = 'past'; $a['str'] = formatDate3($config['dateformat'], (int)$ad[0], (int)$ad[1], (int)$ad[2]); } elseif($diff < -86400) { $a['class'] = 'past'; $a['str'] = sprintf($lang->get('daysago'),ceil(abs($diff)/86400)); } elseif($diff < 0) { $a['class'] = 'past'; $a['str'] = $lang->get('yesterday'); } elseif($diff < 86400) { $a['class'] = 'today'; $a['str'] = $lang->get('today'); } elseif($diff < 172800) { $a['class'] = 'today'; $a['str'] = $lang->get('tomorrow'); } elseif($diff < 691200) { $a['class'] = 'soon'; $a['str'] = sprintf($lang->get('indays'),ceil($diff/86400)); } - elseif($ad[0] == $at[0]) { $a['class'] = 'future'; $a['str'] = $lang->formatMD((int)$ad[1], (int)$ad[2]); } - else { $a['class'] = 'future'; $a['str'] = $lang->formatYMD((int)$ad[0], (int)$ad[1], (int)$ad[2]); } + elseif($ad[0] == $at[0]) { $a['class'] = 'future'; $a['str'] = formatDate3($config['dateformatshort'], (int)$ad[0], (int)$ad[1], (int)$ad[2]); } + else { $a['class'] = 'future'; $a['str'] = formatDate3($config['dateformat'], (int)$ad[0], (int)$ad[1], (int)$ad[2]); } if($config['duedateformat'] == 2) $a['formatted'] = (int)$ad[1].'/'.(int)$ad[2].'/'.$ad[0]; elseif($config['duedateformat'] == 3) $a['formatted'] = $ad[2].'.'.$ad[1].'.'.$ad[0]; + elseif($config['duedateformat'] == 4) $a['formatted'] = $ad[2].'/'.$ad[1].'/'.$ad[0]; else $a['formatted'] = $duedate; return $a; @@ -596,4 +608,25 @@ function myExceptionHandler($e) exit; } +function formatDate3($format, $ay, $am, $ad) +{ + # F - month long, M - month short + # m - month 2-digit, n - month 1-digit + # d - day 2-digit, j - day 1-digit + global $lang; + $ml = $lang->get('months_long'); + $ms = $lang->get('months_short'); + $Y = $ay; + $n = $am; + $m = $n < 10 ? '0'.$n : $n; + $F = $ml[$am-1]; + $M = $ms[$am-1]; + $j = $ad; + $d = $j < 10 ? '0'.$j : $j; + return str_replace( + array('Y','F','M','n','m','d','j'), + array($Y, $F, $M, $n, $m, $d, $j), + $format); +} + ?> \ No newline at end of file diff --git a/src/common.php b/src/common.php index 61aecc5..7063323 100644 --- a/src/common.php +++ b/src/common.php @@ -75,6 +75,9 @@ class Config 'duedateformat' => array('default'=>1, 'type'=>'i'), 'firstdayofweek' => array('default'=>1, 'type'=>'i'), 'session' => array('default'=>'files', 'type'=>'s', 'options'=>array('files','default')), + 'clock' => array('default'=>24, 'type'=>'i', 'options'=>array(12,24)), + 'dateformat' => array('default'=>'j M Y', 'type'=>'s'), + 'dateformatshort' => array('default'=>'j M', 'type'=>'s'), ); public static function save($config) diff --git a/src/db/config.php.default b/src/db/config.php.default index 645bb03..4666c1f 100644 --- a/src/db/config.php.default +++ b/src/db/config.php.default @@ -41,4 +41,9 @@ $config['firstdayofweek'] = 1; # select session handling mechanism: files or default (php default) $config['session'] = 'files'; +# Date/time formats +$config['clock'] = 24; +$config['dateformat'] = 'j M Y'; +$config['dateformatshort'] = 'j M'; + ?> \ No newline at end of file diff --git a/src/index.php b/src/index.php index 4a6c545..92b6d3b 100644 --- a/src/index.php +++ b/src/index.php @@ -17,6 +17,7 @@ if(isset($_COOKIE['sort']) && $_COOKIE['sort'] != '') $sort = (int)$_COOKIE['sor if($config['duedateformat'] == 2) $duedateformat = 'm/d/yy'; elseif($config['duedateformat'] == 3) $duedateformat = 'dd.mm.yy'; +elseif($config['duedateformat'] == 4) $duedateformat = 'dd/mm/yy'; else $duedateformat = 'yy-mm-dd'; if(!isset($config['firstdayofweek']) || !is_int($config['firstdayofweek']) || @@ -65,7 +66,7 @@ $().ready(function(){ if(is_logged()) echo "\tflag.isLogged = true;\n"; echo "\tupdateAccessStatus();\n"; } - echo "\tloadLists(1)\n"; + echo "\tloadLists(1);\n"; ?> preloadImg(); $("#duedate").datepicker({dateFormat: '', firstDay: , diff --git a/src/lang/class.default.php b/src/lang/class.default.php index 2fa127d..5008cfa 100644 --- a/src/lang/class.default.php +++ b/src/lang/class.default.php @@ -57,8 +57,6 @@ class DefaultLang 'months_short' => array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"), 'months_long' => array("January","February","March","April","May","June","July","August","September","October","November","December"), 'days_min' => array("Su","Mo","Tu","We","Th","Fr","Sa"), - 'date_md' => "%1\$s %2\$d", - 'date_ymd' => "%2\$s %3\$d, %1\$d", 'today' => "today", 'yesterday' => "yesterday", 'tomorrow' => "tomorrow", @@ -110,18 +108,6 @@ class DefaultLang if(isset($this->default_inc[$key])) return $this->default_inc[$key]; return $key; } - - function formatMD($m, $d) - { - $months = $this->get('months_short'); - return sprintf($this->get('date_md'), $months[$m-1], $d); - } - - function formatYMD($y, $m, $d) - { - $months = $this->get('months_short'); - return sprintf($this->get('date_ymd'), $y, $months[$m-1], $d); - } } ?> \ No newline at end of file diff --git a/src/lang/en.php b/src/lang/en.php index 49d4fd7..853387a 100644 --- a/src/lang/en.php +++ b/src/lang/en.php @@ -52,8 +52,6 @@ class Lang extends DefaultLang 'months_short' => array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"), 'months_long' => array("January","February","March","April","May","June","July","August","September","October","November","December"), 'days_min' => array("Su","Mo","Tu","We","Th","Fr","Sa"), - 'date_md' => "%1\$s %2\$d", - 'date_ymd' => "%2\$s %3\$d, %1\$d", 'today' => "today", 'yesterday' => "yesterday", 'tomorrow' => "tomorrow",