ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilFormat.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
23{
24 function ilFormat ()
25 {
26 return;
27 }
28
29 // Holt das aktuelle Datum und gibt es im Format TT.MM.JJJJ zurck
30 function getDateDE ()
31 {
32 $date = getdate();
33 $datum = sprintf("%02d.%02d.%04d", $date["mday"],$date["mon"],$date["year"]);
34 return $datum;
35 }
36
45 function input2date ($AInputDate)
46 {
47
48 $date=""; $y=""; $m=""; $d="";
49// if (ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})",$idate,$p))
50 if (ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})",$AInputDate,$p))
51 {
52
53 $d = $p[1];
54 $m = $p[2];
55 $y = $p[3];
56
57 if (($d>0 && $d<32) && ($m>0 && $m<13) && (strlen($y)!=3))
58 {
59 if (strlen($d) == 1) $d = "0".$d;
60 if (strlen($m) == 1) $m = "0".$m;
61
62 if (strlen($y) == 2)
63 {
64 if ($y>=70) $y = $y + 1900;
65 if ($y<70) $y = $y + 2000;
66 }
67
68 // is valid?
69 checkdate($m, $d, $y);
70
71 // Ausgabe mit Uhrzeit
72
73 // Uhrzeit holen
74 $uhrzeit = substr($AInputDate, -8);
75
76 // Uhrzeit auf Gltigkeit prfen
77 if (ereg("([0-9]{2}):([0-9]{2}):([0-9]{2})",$AInputDate,$p))
78 {
79 $h = $p[1];
80 $min = $p[2];
81 $s = $p[3];
82
83 if (($h>-1 && $h<24) && ($min>-1 && $min<60) && ($s>-1 && $s<60))
84 {
85 // Uhrzeit stimmt/ist vorhanden
86 $date = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$y,$m,$d,$h,$min,$s);
87 }
88 }
89 else
90 {
91 // Uhrzeit ist falsch/fehlt; hnge aktuelle Zeit an
92 $zeit = getdate();
93 $date = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$y,$m,$d,$zeit["hours"],$zeit["minutes"],$zeit["seconds"]);
94 }
95 // Ausgabe ohne Uhrzeit
96 //$date = sprintf("%04d-%02d-%02d",$y,$m,$d);
97 return $date;
98 }
99 }
100 }
101
102
107 function dateDB2timestamp ($ADatumSQL)
108 {
109 $timestamp = substr($ADatumSQL, 0, 4).
110 substr($ADatumSQL, 5, 2).
111 substr($ADatumSQL, 8, 2).
112 substr($ADatumSQL, 11, 2).
113 substr($ADatumSQL, 14, 2).
114 substr($ADatumSQL, 17, 2);
115
116 return $timestamp;
117 }
118
123 function dateDE2timestamp ($ADatum)
124 {
125 $timestamp = substr($ADatum, 6, 4).
126 substr($ADatum, 3, 2).
127 substr($ADatum, 0, 2).
128 substr($ADatum, 11, 2).
129 substr($ADatum, 14, 2).
130 substr($ADatum, 17, 2);
131
132 return $timestamp;
133 }
134
135
141 {
142 return sprintf("%02d.%02d.%04d",substr($t, 8, 2),substr($t, 5, 2),substr($t, 0, 4));
143 }
144
145
151 {
152 return sprintf("%02d.%02d.%04d",substr($t, 6, 2),substr($t, 4, 2),substr($t, 0, 4));
153 }
154
155
161 {
162 return sprintf("%02d.%02d.%04d %02d:%02d:%02d",substr($t, 6, 2),substr($t, 4, 2),substr($t, 0, 4),substr($t, 8, 2),substr($t, 10, 2),substr($t, 12, 2));
163 }
164
165
171 {
172 return sprintf("%04d-%02d-%02d",substr($t, 0, 4),substr($t, 4, 2),substr($t, 6, 2));
173 }
174
181 function ftimestamp2datetimeDB($aTimestamp)
182 {
183 $date = "";
184 if (preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $aTimestamp, $matches))
185 {
186 $date = "$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:$matches[6]";
187 }
188 return $date;
189 }
190
191
199 function compareDates ($ADate1,$ADate2)
200 {
201 if ($ADate1 > $ADate2)
202 {
203 return $ADate1;
204 }
205
206 return $ADate2;
207 }
208
209
214 function checkDecimal ($var)
215 {
216 return doubleval(ereg_replace (",",".",$var));
217 }
218
219
223 function fGeld ()
224 {
225 $num_args = func_num_args();
226
227 $geld = func_get_arg(0);
228
229 if ($num_args == 1)
230 {
231 $test = intval($geld);
232
233 if (!$test)
234 return "&nbsp;";
235 }
236
237 return number_format($geld,2,",",".")." &euro;";
238 }
239
240
245 function fProzent ()
246 {
247 $num_args = func_num_args();
248
249 $prozent = func_get_arg(0);
250
251 if ($num_args == 1)
252 {
253 $test = intval($prozent);
254
255 if (!$test)
256 return "&nbsp;";
257 }
258
259 return number_format($prozent,2,",",".")."%";
260 }
261
266 function runden ($value)
267 {
268 return round($value * 100) / 100;
269 }
270
288 public static function fmtDateTime($a_str,$a_dateformat,$a_timeformat,$a_mode = "datetime", $a_relative = TRUE)
289 {
290 //no format defined. set to default format
291 if ($a_dateformat == "")
292 {
293 $a_dateformat = "Y-m-d";
294 }
295
296 // same for time format
297 if ($a_timeformat == "")
298 {
299 $a_timeformat = "H:i:s";
300 }
301
302 // The sql-date 0000-00-00 00:00:00 means "no-date given"
303 if ($a_str == '0000-00-00 00:00:00')
304 {
305 global $lng;
306 return $lng->txt('no_date');
307 }
308 //
309 //get values from given sql-date
310 $d = substr($a_str,8,2);
311 $m = substr($a_str,5,2);
312 $y = substr($a_str,0,4);
313 $h = substr($a_str,11,2);
314 $i = substr($a_str,14,2);
315 $s = substr($a_str,17,4);
316
317 // Minimum date is 1.1.1970
318 if(($y < 1970) or
319 ($y == 1970 and ($m < 1 or $d < 1)))
320 {
321 $y = 1970;
322 $m = 1;
323 $d = 2;
324 $h = $i = $s = 0;
325 }
326
327 if ($a_mode == "time")
328 {
329 return date($a_timeformat,mktime($h,$i,$s,1,1,1999));
330 }
331
332 // BEGIN WebDAV: Display relative date.
333 $timestamp = mktime($h,$i,$s,$m,$d,$y);
334 $now = time();
335 $minuteswest = gettimeofday(false);
336 $minuteswest = $minuteswest['minuteswest'];
337 $today = $now - $now % (24 * 60 * 60) + $minuteswest * 60;
338 $isToday = $today <= $timestamp && $timestamp < $today + 24 * 60 * 60;
339 $isYesterday = $today - 24 * 60 * 60 <= $timestamp && $timestamp < $today;
340 $isTomorrow = $today + 24 * 60 * 60 <= $timestamp && $timestamp < $today + 48 * 60 * 60;
341
342 global $lng;
343 if ($a_relative)
344 {
345 $date = ($isToday) ? $lng->txt('today') :
346 (($isYesterday) ? $lng->txt('yesterday') :
347 (($isTomorrow) ? $lng->txt('tomorrow') :
348 date($a_dateformat,mktime($h,$i,$s,$m,$d,$y))))
349 ;
350 }
351 else
352 {
353 $date = date($a_dateformat,mktime($h,$i,$s,$m,$d,$y));
354 }
355
356 return ($a_mode == "date") ? $date : $date.' '.date($a_timeformat,mktime($h,$i,$s,$m,$d,$y));
357 // END WebDAV: Display relative date.
358 }
359
373 function fmtFloat($a_float, $a_decimals=0, $a_dec_point = null, $a_thousands_sep = null, $a_suppress_dot_zero=false)
374 {
375 global $lng;
376
377
378 if ($a_dec_point == null)
379 {
380 $a_dec_point = $lng->txt('lang_sep_decimal');
381 {
382 $a_dec_point = ".";
383 }
384 }
385 if ($a_dec_point == '-lang_sep_decimal-')
386 {
387 $a_dec_point = ".";
388 }
389
390 if ($a_thousands_sep == null)
391 {
392 $a_thousands_sep = $lng->txt('lang_sep_thousand');
393 {
394 $a_th = ",";
395 }
396 }
397 if ($a_thousands_sep == '-lang_sep_thousand-')
398 {
399 $a_thousands_sep = ",";
400 }
401
402 $txt = number_format($a_float, $a_decimals, $a_dec_point, $a_thousands_sep);
403
404 // remove trailing ".0"
405 if (($a_suppress_dot_zero == 0 || $a_decimal == 0) &&
406 substr($txt,-2) == $a_dec_point.'0')
407 {
408 $txt = substr($txt, 0, strlen($txt) - 2);
409 }
410 if ($a_float == 0 and $txt == "")
411 {
412 $txt = "0";
413 }
414 return $txt;
415 }
416
417 function unixtimestamp2datetime($a_unix_timestamp = "")
418 {
419 if (strlen($a_unix_timestamp) == 0)
420 {
421 return strftime("%Y-%m-%d %H:%M:%S");
422 }
423 else
424 {
425 return strftime("%Y-%m-%d %H:%M:%S", $a_unix_timestamp);
426 }
427 }
428
440 function formatDate($a_date,$a_mode = "datetime", $a_omit_seconds = false, $a_relative = TRUE)
441 {
442 global $lng;
443
444 // return when no datetime is given
445 if ($a_date == "0000-00-00 00:00:00")
446 {
447 return $lng->txt("no_date");
448 }
449
450 $dateformat = $lng->txt("lang_dateformat");
451 if ($a_omit_seconds && ($lng->txt("lang_timeformat_no_sec") != "-lang_timeformat_no_sec-"))
452 {
453 $timeformat = $lng->txt("lang_timeformat_no_sec");
454 }
455 else
456 {
457 $timeformat = $lng->txt("lang_timeformat");
458 }
459
460 if ($dateformat == "-lang_dateformat-")
461 {
462 $dateformat = "";
463 }
464
465 if ($timeformat == "-lang_timeformat-")
466 {
467 $timeformat = "";
468 }
469
470 return ilFormat::fmtDateTime($a_date,$dateformat,$timeformat,$a_mode, $a_relative);
471 }
472
473 function formatUnixTime($ut,$with_time = false)
474 {
475 global $lng;
476
477 $format = $lng->txt('lang_dateformat');
478
479 if($with_time)
480 {
481 $format .= (' '.$lng->txt('lang_timeformat_no_sec'));
482 }
483 return date($format,$ut);
484 }
485 /*
486 * calculates the difference between 2 unix timestamps and
487 * returns a proper formatted output
488 *
489 * @param integer unix timestamp1
490 * @param integer unix timestamp2
491 * @return string time difference in hh:mm:ss
492 */
493 function dateDiff($a_ts1,$a_ts2)
494 {
495 $r = $a_ts2 - $a_ts1;
496
497 $dd = floor($r/86400);
498
499 if ($dd <= 9)
500 $dd = "0".$dd;
501
502 $r = $r % 86400;
503
504 $hh = floor($r/3600);
505
506 if ($hh <= 9)
507 $hh = "0".$hh;
508
509 $r = $r % 3600;
510
511 $mm = floor($r/60) ;
512
513 if ($mm <= 9)
514 $mm = "0".$mm;
515
516 $r = $r % 60;
517 $ss = $r;
518
519 if ($ss <= 9)
520 $ss = "0".$ss;
521
522 return $hh.":".$mm.":".$ss;
523 }
524
531 function datetime2unixTS($a_datetime)
532 {
533 $arrDT = explode(" ", $a_datetime);
534 $arrD = explode("-", $arrDT[0]);
535 $arrT = explode(":", $arrDT[1]);
536
537 return mktime($arrT[0], $arrT[1], $arrT[2], $arrD[1], $arrD[2], $arrD[0]);
538 }
539
549 function _secondsToString($seconds, $force_with_seconds = false, $a_lng = null)
550 {
551 global $lng;
552
553 if($a_lng)
554 {
555 $lng = $a_lng;
556 }
557
558 $seconds = $seconds ? $seconds : 0;
559
560 // #13625
561 if($seconds > 0)
562 {
563 $days = floor($seconds / 86400);
564 $rest = $seconds % 86400;
565
566 $hours = floor($rest / 3600);
567 $rest = $rest % 3600;
568
569 $minutes = floor($rest / 60);
570 $seconds = $rest % 60;
571 }
572 else
573 {
574 $days = ceil($seconds / 86400);
575 $rest = $seconds % 86400;
576
577 $hours = ceil($rest / 3600);
578 $rest = $rest % 3600;
579
580 $minutes = ceil($rest / 60);
581 $seconds = $rest % 60;
582 }
583
584 if($days)
585 {
586 $message = $days . ' '. ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
587 }
588 if($hours)
589 {
590 if($message)
591 {
592 $message .= ' ';
593 }
594 $message .= ($hours . ' '. ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
595 }
596 if($minutes)
597 {
598 if($message)
599 {
600 $message .= ' ';
601 }
602 $message .= ($minutes . ' '. ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
603 }
604 if($force_with_seconds && $seconds)
605 {
606 if($message)
607 {
608 $message .= ' ';
609 }
610 $message .= ($seconds . ' '. ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
611 }
612 if(!$days and !$hours and !$minutes)
613 {
614 return $seconds .' '. ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
615 }
616 else
617 {
618 return $message;
619 }
620 }
628 function _secondsToShortString($seconds)
629 {
630 global $lng;
631
632 $seconds = $seconds ? $seconds : 0;
633
634 global $lng;
635
636 $days = floor($seconds / 86400);
637 $rest = $seconds % 86400;
638
639 $hours = floor($rest / 3600);
640 $rest = $rest % 3600;
641
642 $minutes = floor($rest / 60);
643 $rest = $rest % 60;
644
645 return sprintf("%02d:%02d:%02d:%02d",$days,$hours,$minutes,$rest);
646
647 }
648
654 static function _getLocalMoneyFormat($float_number)
655 {
656 global $ilias;
657
658 $language = $ilias->getSetting("language");
659 $money_locale = $language.'_'.strtoupper($language);
660 /* de_DE en_US en_EN fr_FR .UTF-8
661 */ //$money_locale = 'de_DE.UTF-8';
662 //vd($_SERVER['HTTP_ACCEPT_LANGUAGE']);
663
664 setlocale(LC_MONETARY, $money_locale);
665 return $float_number;
666 //return money_format('%!2n', $float_number);
667 }
668
681 public static function _getSizeMagnitude()
682 {
683 return 1024;
684 }
692 public static function formatFloat($size, $a_decimals, $a_suppress_dot_zero=false, $a_mode = 'short', $a_lng = null)
693 {
694 global $lng;
695 if ($a_lng == null) {
696 $a_lng = $lng;
697 }
698 return self::fmtFloat($size, $a_decimals, $a_lng->txt('lang_sep_decimal'), $a_lng->txt('lang_sep_thousand', $a_suppress_dot_zero), true).' '.$a_lng->txt($scaled_unit);
699 }
716 public static function formatSize($size, $a_mode = 'short', $a_lng = null)
717 {
718 global $lng;
719 if ($a_lng == null) {
720 $a_lng = $lng;
721 }
722
723 $result;
725
726 $scaled_size;
727 $scaled_unit;
728
729 if ($size >= $mag * $mag * $mag)
730 {
731 $scaled_size = $size/$mag/$mag/$mag;
732 $scaled_unit = 'lang_size_gb';
733 }
734 else if ($size >= $mag * $mag)
735 {
736 $scaled_size = $size/$mag/$mag;
737 $scaled_unit = 'lang_size_mb';
738 }
739 else if ($size >= $mag)
740 {
741 $scaled_size = $size/$mag;
742 $scaled_unit = 'lang_size_kb';
743 }
744 else
745 {
746 $scaled_size = $size;
747 $scaled_unit = 'lang_size_bytes';
748 }
749
750 $result = self::fmtFloat($scaled_size,($scaled_unit == 'lang_size_bytes') ? 0:1, $a_lng->txt('lang_sep_decimal'), $a_lng->txt('lang_sep_thousand'), true).' '.$a_lng->txt($scaled_unit);
751 if ($a_mode == 'long' && $size > $mag)
752 {
753 $result .= ' ('.
754 self::fmtFloat($size,0,$a_lng->txt('lang_sep_decimal'),$a_lng->txt('lang_sep_thousand')).
755 ' '.$a_lng->txt('lang_size_bytes').')';
756 }
757 return $result;
758 }
759}
760
761?>
$result
$size
Definition: RandomTest.php:79
$test
Definition: Utf8Test.php:85
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
input2date($AInputDate)
Prft eingegebes Datum und wandelt es in DB-konformen Syntax um Eingabe: TT.MM.JJJJ oder T....
static formatFloat($size, $a_decimals, $a_suppress_dot_zero=false, $a_mode='short', $a_lng=null)
Returns the specified float in human friendly form.
formatDate($a_date, $a_mode="datetime", $a_omit_seconds=false, $a_relative=TRUE)
format a date according to the user language shortcut for Format::fmtDateTime @access public
static fmtDateTime($a_str, $a_dateformat, $a_timeformat, $a_mode="datetime", $a_relative=TRUE)
formatting function for dates
dateDiff($a_ts1, $a_ts2)
unixtimestamp2datetime($a_unix_timestamp="")
ftimestamp2datetimeDE($t)
formats timestamp to german datetime
static _getSizeMagnitude()
Returns the magnitude used for size units.
ftimestamp2dateDB($t)
formats timestamp to db-date
ftimestamp2dateDE($t)
formats timestamp to german date
compareDates($ADate1, $ADate2)
Datum vergleichen Erwartet timestamps Liefert das aktuellere Datum als Timestamp zurck.
fProzent()
formatiert Prozentzahlen (Format: 00,00%).
fmtFloat($a_float, $a_decimals=0, $a_dec_point=null, $a_thousands_sep=null, $a_suppress_dot_zero=false)
format a float
_secondsToString($seconds, $force_with_seconds=false, $a_lng=null)
converts seconds to string: Long: 7 days 4 hour(s) ...
fdateDB2dateDE($t)
formats db-datetime to german date
runden($value)
Floats auf 2 Nachkommastellen runden.
datetime2unixTS($a_datetime)
converts datetime to a unix timestamp
ftimestamp2datetimeDB($aTimestamp)
Timestamp to database datetime.
dateDB2timestamp($ADatumSQL)
db-datetime to timestamp
dateDE2timestamp($ADatum)
German datetime to timestamp.
fGeld()
formatiert Geldwerte (Format: 00,00 + Eurosymbol).
static _getLocalMoneyFormat($float_number)
converts a float number to money format, depending on system language
checkDecimal($var)
Prft Zahlen mit Nachkommastellen und erlaubt ein Komma als Nachstellentrenner.
static formatSize($size, $a_mode='short', $a_lng=null)
Returns the specified file size value in a human friendly form.
formatUnixTime($ut, $with_time=false)
_secondsToShortString($seconds)
converts seconds to string: Long: 7 days 4 hour(s) ...
$txt
Definition: error.php:12
$y
Definition: example_007.php:83
$h
$r
Definition: example_031.php:79
$rest
Definition: goto.php:85
global $lng
Definition: privfeed.php:40