PageRenderTime 76ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/include/helpers/inc_date_format_functions.php

https://github.com/timschofield/2.8
PHP | 231 lines | 141 code | 50 blank | 40 comment | 39 complexity | 443c63540596efbc9763d76b11d2fa45 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php
  2. /*------begin------ This protection code was suggested by Luki R. luki@karet.org ---- */
  3. if (stristr($PHP_SELF,'inc_date_format_functions.php')) die('<meta http-equiv="refresh" content="0; url=../">');
  4. /*------end------*/
  5. /**
  6. * getDateFormat gets the date format stored in the databank
  7. * if unsuccesful, it will get the default from the defaults.ini
  8. * param $link = receives the link created by the db connection routine
  9. * param $DBLink_OK = receives the tag created by the db connection routine
  10. * return = the date format
  11. */
  12. function getDateFormat()
  13. {
  14. global $db, $dblink_ok;
  15. $errFormat=0;
  16. /* If no link to db, make own link*/
  17. if(!isset($db) || !$db) include_once(CARE_BASE .'include/helpers/inc_db_makelink.php');
  18. if($dblink_ok)
  19. {
  20. $sql="SELECT value AS date_format FROM care_config_global WHERE type='date_format'";
  21. if($result=$db->Execute($sql)) {
  22. if($result->RecordCount()) {
  23. $df=$result->FetchRow();
  24. return $df['date_format'];
  25. }
  26. else $errFormat=1;
  27. }
  28. else $errFormat=1;
  29. }
  30. else $errFormat=1;
  31. if($errFormat) {
  32. $df=get_meta_tags(CARE_BASE .'global_conf/format_date_default.pid');
  33. if($df['date_format']!='') return $df['date_format'];
  34. else return 'dd.MM.yyyy'; // this is the last alternative format (german traditional)
  35. }
  36. }
  37. /**
  38. * format2Local converts the standard YYYY-MM-DD format to the local format
  39. * param $stdDate = receives the standard YYYY-MM-DD formatted date
  40. * param $localFromat = receives the local format
  41. * param $retTime = if 1, will append the time part to the returned date
  42. * param $sepChars = will accept the reference to an array containing the separator chars list. if empty, the default will be used
  43. * return = the date in local format
  44. * The function assumes that the dates are in correct formats
  45. * therefore a validation routine must be done at the client side
  46. */
  47. function formatDate2Local($stdDate, $localFormat, $retTime=FALSE, $timeOnly=FALSE, &$sepChars)
  48. {
  49. global $lang;
  50. if(!$sepChars) $sepChars=array('-','.','/',':',',');
  51. $localFormat=strtolower($localFormat);
  52. if(stristr('0000',$stdDate)) return strtr($localFormat,'yYmMdDHis','000000000'); // IF std date is 0 return 0's in local format
  53. /* If time is included then isolate */
  54. if(strchr($stdDate,':'))
  55. {
  56. list($stdDate,$stdTime) = explode(' ',$stdDate);
  57. if($timeOnly) return $stdTime; /* If time only is needed */
  58. }
  59. $stdArray=explode('-',$stdDate);
  60. /* Detect time separator and explode localFormat */
  61. for($i=0;$i<sizeof($sepChars);$i++)
  62. {
  63. if(strchr($localFormat,$sepChars[$i]))
  64. {
  65. $localSeparator=$sepChars[$i];
  66. $localArray=explode($localSeparator,$localFormat);
  67. break;
  68. }
  69. }
  70. for($i=0;$i<3;$i++)
  71. {
  72. if($localArray[$i]=='yyyy') $localArray[$i]=$stdArray[0];
  73. elseif($localArray[$i]=='mm') $localArray[$i]=$stdArray[1];
  74. elseif($localArray[$i]=='dd') $localArray[$i]=$stdArray[2];
  75. }
  76. //if ($lang=='de') $stdTime=strtr($stdTime,':','.'); // This is a hard coded time format translator for german "de" language
  77. if($retTime) return implode($localSeparator,$localArray).' '.$stdTime;
  78. else return implode($localSeparator,$localArray);
  79. }
  80. function formatShortDate2Local($month,$day,$localFormat)
  81. {
  82. if(!$sepChars) $sepChars=array('-','.','/',':',',');
  83. $localFormat=strtolower($localFormat);
  84. /* Detect time separator and explode localFormat */
  85. for($i=0;$i<sizeof($sepChars);$i++)
  86. {
  87. if(strchr($localFormat,$sepChars[$i]))
  88. {
  89. $localSeparator=$sepChars[$i];
  90. $localArray=explode($localSeparator,$localFormat);
  91. break;
  92. }
  93. }
  94. for($i=0;$i<3;$i++)
  95. {
  96. if($localArray[$i]=='yyyy') $s_tag=$i;
  97. elseif($localArray[$i]=='mm') $localArray[$i]=$month;
  98. elseif($localArray[$i]=='dd') $localArray[$i]=$day;
  99. }
  100. array_splice($localArray,$s_tag,1);
  101. return implode($localSeparator,$localArray);
  102. }
  103. function formatDate2STD($localDate,$localFormat,&$sepChars)
  104. {
  105. $finalDate=0;
  106. $localFormat=strtolower($localFormat);
  107. if(!$sepChars) $sepChars=array('-','.','/',':',',');
  108. if(stristr('0000',$finalDate)) $finalDate=0;
  109. if(!$finalDate)
  110. {
  111. for($i=0;$i<sizeof($sepChars);$i++)
  112. {
  113. if(strchr($localDate,$sepChars[$i]))
  114. {
  115. $loc_array=explode($sepChars[$i],$localDate);
  116. break;
  117. }
  118. }
  119. for($i=0;$i<sizeof($sepChars);$i++)
  120. {
  121. if(strchr($localFormat,$sepChars[$i]))
  122. {
  123. $Format_array=explode($sepChars[$i],$localFormat);
  124. break;
  125. }
  126. }
  127. /* Detect local format and reformat the local time to DATE standard */
  128. for($i=0;$i<3;$i++)
  129. {
  130. if($Format_array[$i]=='yyyy') { $vYear = $loc_array[$i];}
  131. elseif($Format_array[$i]=='mm') { $vMonth = $loc_array[$i];}
  132. elseif($Format_array[$i]=='dd') { $vDay = $loc_array[$i];}
  133. }
  134. # if invalid numeric return empty string
  135. if(!is_numeric($vYear)||!is_numeric($vMonth)||!is_numeric($vDay)){
  136. $finalDate= '';
  137. }else{
  138. # DATE standard
  139. if(strlen($vMonth)==1) $vMonth='0'.$vMonth;
  140. if(strlen($vDay)==1) $vDay='0'.$vDay;
  141. $finalDate=$vYear.'-'.$vMonth.'-'.$vDay;
  142. }
  143. }
  144. return $finalDate;
  145. }
  146. /**
  147. * convertTimeStandard() will return a time in the format HH:mm:ss
  148. * param $time_val = the time value to be converted
  149. * return = the time in the format HH:mm:ss
  150. */
  151. function convertTimeToStandard($time_val)
  152. {
  153. $time_val=strtr($time_val,'.,/-','::::'); // convert the separators to ':'
  154. $sep_count=substr_count($time_val,':');
  155. switch($sep_count)
  156. {
  157. case '': $time_val.=':00:00'; break;
  158. case 0: $time_val.=':00:00'; break;
  159. case 1: $time_val.=':00';
  160. }
  161. return $time_val;
  162. }
  163. /**
  164. * convertTimeLocal() will return a time in the local format
  165. * param $time_val = the time value to be converted in HHxMMxSS, where x is the separator which will be converted to ":"
  166. * return = the time in the format HH:mm:ss
  167. */
  168. function convertTimeToLocal($time_val)
  169. {
  170. global $lang;
  171. switch($lang)
  172. {
  173. //case 'de': $time_val=strtr($time_val,':,/-','....'); break; # convert the separators to '.'
  174. default : $time_val=strtr($time_val,'.,/-','::::'); # convert the separators to ':'
  175. }
  176. //return $time_val;
  177. return substr($time_val,0,strrpos($time_val,':'));
  178. }
  179. # Now load the date format
  180. $date_format=getDateFormat();
  181. ?>