2
/******************************************************************************************************
3
/ Class SDTO - Simple DateTime Object
4
/ A simple way to handle times and dates and to convert between
5
/ System and SQL Formats
7
/ (C) 2001-2004 Christian Hansel, CVH, chris@cpi-service.com
9
/ Distributed under GPL, may be distributed, modified and utilized if Copyright
10
/ notice is maintained and manual is provided and author is notified of modifications
12
/ Please support our development by sending remarks or comments or suggestions
14
/********************************************************************************************************/
16
class SimpleDateTimeObject {
17
var $basetime; // @private - holds UniX Timestamp
18
var $calctime; // @private - holds UniX Timestamp Calculations
19
var $stdParamDateList; // @private - holds StandardParameters for Dropdown-lists
20
var $stdParamAdd; // @private - holds StandardParameters for Adding/Substracting Periods
23
function SimpleDateTimeObject ($timestamp = -1) {
24
$this->basetime = ($timestamp == -1) ? mktime() : $timestamp;
25
$this->calctime = $this->basetime;
26
$this->stdParamDateList = array( "start" => 0,
30
"format" =>"d.M.Y H:i",
34
$this->stdParamAdd = array( "years"=>0,
44
/****************************************************************************************************
45
/ @public Time2Array -
46
/ Helper Function which Transforms a Time-String
47
/ in SQL Format into an Array of Periods
48
/ Parameters $timestring - e.g. "-01:00:00)
50
/********************************************************************************************************/
53
function Time2Array($timestring) {
54
$timestring = preg_replace("#\s#","",$timestring);
55
$timestring = preg_replace("#\:#","",$timestring);
56
$timestring = preg_replace("#\.#","",$timestring);
57
$timestring = preg_replace("#\-#","",$timestring);
58
$timestring = preg_replace("#\/#","",$timestring);
59
$fac = ("-" == substr($timestring,0,1)) ? -1 : 1;
61
$timestring = ("-" == substr($timestring,0,1) || "+" == substr($timestring,0,1)) ? substr($timestring,1,strlen($timestring)-1) : $timestring;
63
$array['hours'] = $fac * substr($timestring, 0,2);
64
$array['minutes'] = $fac * substr($timestring, 2,2) ;
65
$array['seconds'] = (4 < strlen($timestring)) ? $fac * substr($timestring, 4,2) : 0;
70
/****************************************************************************************************
71
/ @public DateTime2Array -
72
/ Helper Function which Transforms a DateTime-String
73
/ in SQL Format into an Array of Periods
74
/ Parameters $timestring - e.g. "12.01.2004 23:00:00)
76
/********************************************************************************************************/
77
function DateTime2Array($timestring) {
78
$timestring = preg_replace("#\s#","",$timestring);
79
$timestring = preg_replace("#\:#","",$timestring);
80
$timestring = preg_replace("#\.#","",$timestring);
81
$timestring = preg_replace("#\/#","",$timestring);
82
$timestring = preg_replace("#\-#","",$timestring);
83
$fac = ("-" == substr($timestring,0,1)) ? -1 : 1;
84
$timestring = ("-" == substr($timestring,0,1)) ? substr($timestring,1,strlen($timestring)-1) : $timestring;
86
$array['years'] = (6==strlen($timestamp)) ? $fac * substr($timestamp,0,2): $fac * substr($timestamp,0,4);
87
$array['months'] = (6==strlen($timestamp)) ? $fac * substr($timestamp,2,2): $fac * substr($timestamp,4,2);
88
$array['days'] = (6==strlen($timestamp)) ? $fac * substr($timestamp,4,2): $fac * substr($timestamp,6,2);
89
$array['hours'] = (12 > strlen($timestamp)) ? 0 : $fac * substr($timestamp, 8,2);
90
$array['minutes'] = (12 > strlen($timestamp)) ? 0 : $fac * substr($timestamp, 10,2);
91
$array['seconds'] = (14 > strlen($timestamp)) ? 0 : $fac * substr($timestamp, 12,2);
97
/****************************************************************************************************
99
/ Adds a Period to the BaseTime of the Object
100
/ Returns a Unix Timestamp
101
/ Parameters $param [Array]- e.g.array("days"=>2, "months"=> 1, "hours"=>4)
102
/ optional $overwrite [Bool] = true - If true the BaseTime of the
103
/ Object will be overwritten with
104
/ or adjusted to the calculated result
106
/********************************************************************************************************/
107
function add($param = array(), $overwrite = true) {
108
$base_day = date ("d",$this->basetime);
109
$base_month = date ("m",$this->basetime);
110
$base_year = date ("Y",$this->basetime);
111
$base_hour = date ("H",$this->basetime);
112
$base_minute = date ("i",$this->basetime);
113
$base_second = date ("s",$this->basetime);
115
$hours = (isset($param['hours']) && ! empty($param['hours'])) ? $param['hours'] : $this->stdParamAdd['hours'];
116
$minutes = (isset($param['minutes']) && ! empty($param['minutes'])) ? $param['minutes'] : $this->stdParamAdd['minutes'];
117
$seconds = (isset($param['seconds']) && ! empty($param['seconds'])) ? $param['seconds'] : $this->stdParamAdd['seconds'];
118
$days = (isset($param['days']) && ! empty($param['days'])) ? $param['days'] : $this->stdParamAdd['days'];
119
$months = (isset($param['months']) && ! empty($param['months'])) ? $param['months'] : $this->stdParamAdd['months'];
120
$years = (isset($param['years']) && ! empty($param['years'])) ? $param['years'] : $this->stdParamAdd['years'];
123
$this->calctime = mktime ( $base_hour + $hours,
124
$base_minute + $minutes,
125
$base_second + $seconds,
126
$base_month + $months,
128
$base_year + $years);
130
$this->basetime = $this->calctime;
136
return $this->calctime;
141
/****************************************************************************************************
143
/ Subtracts a Period from the BaseTime of the Object
144
/ Returns a Unix Timestamp
145
/ Parameters $param [Array]- e.g.array("days"=>2, "months"=> 1, "hours"=>4)
146
/ optional $overwrite [Bool] = true - If true the BaseTime of the
147
/ Object will be overwritten with
148
/ or adjusted to the calculated result
150
/********************************************************************************************************/
151
function sub($param = array(), $overwrite = true) {
152
$base_day = date ("d",$this->basetime);
153
$base_month = date ("m",$this->basetime);
154
$base_year = date ("Y",$this->basetime);
155
$base_hour = date ("H",$this->basetime);
156
$base_minute = date ("i",$this->basetime);
157
$base_second = date ("s",$this->basetime);
159
$hours = (isset($param['hours']) && ! empty($param['hours'])) ? $param['hours'] : $this->stdParamAdd['hours'];
160
$minutes = (isset($param['minutes']) && ! empty($param['minutes'])) ? $param['minutes'] : $this->stdParamAdd['minutes'];
161
$seconds = (isset($param['seconds']) && ! empty($param['seconds'])) ? $param['seconds'] : $this->stdParamAdd['seconds'];
162
$days = (isset($param['days']) && ! empty($param['days'])) ? $param['days'] : $this->stdParamAdd['days'];
163
$months = (isset($param['months']) && ! empty($param['months'])) ? $param['months'] : $this->stdParamAdd['months'];
164
$years = (isset($param['years']) && ! empty($param['years'])) ? $param['years'] : $this->stdParamAdd['years'];
167
$this->calctime = mktime ( $base_hour - $hours,
168
$base_minute - $minutes,
169
$base_second - $seconds,
170
$base_month - $months,
172
$base_year - $years);
174
$this->basetime = $this->calctime;
180
return $this->calctime;
186
/****************************************************************************************************
187
/ @public diff_MySQL -
188
/ Calculates the Difference between a MYSQL Timestamp
189
/ and the Objects Basetime which is useful for SQL commands
190
/ like "SELECT DAT_ADD(mydate, INTERVAL '10 02' DAY_HOUR)
191
/ or to be used in PHP/HTML : print "The time you have to do this job is ".$result['days'];
192
/ returns an array like Array { "years" => 1 ,
198
/ when the Parameter $timestring lies 1 year and 5 weeks and 3 days and 35 minutes after
199
/ the Basetime of the Object }
200
/ Parameters $timestring - A MysQL Dat/DateTime String : e.g. "20040711132712"
201
/ $allow_negative - if true negative Differences will be returned as such
202
/ otherwise differences are always positive
204
/********************************************************************************************************/
206
function diff_MySQL($timestring, $allow_negative = false) {
207
$basetime = $this->basetime; // 2BRemembered
208
$target = $this->setMySQLDateTime($timestring);
209
if ($target > $basetime) {
210
$diff = $target - $basetime ;
213
$diff = $basetime-$target;
214
$fac = ($allow_negative)? -1 : 1;
216
$diffarr['years'] = $fac * $this->extract_from_seconds($diff,"years");
217
$diffarr['weeks'] = $fac * $this->extract_from_seconds($diff,"weeks");
218
$diffarr['days'] = $fac * $this->extract_from_seconds($diff,"days");
219
$diffarr['hours'] = $fac * $this->extract_from_seconds($diff,"hours");
220
$diffarr['minutes'] = $fac * $this->extract_from_seconds($diff,"minutes");
221
$diffarr['seconds'] = $fac * $this->extract_from_seconds($diff,"seconds");
222
$this->basetime = $basetime;
228
/****************************************************************************************************
229
/ @private / public extract_from_seconds -
230
/ extracts full years / days / weeks / hours / or minutes from a Unix Timestamp a
231
/ any period given seconds
232
/ returns an integer of the time unit provided as parameter 2 (e.g. 'hours')
233
/ Parameters $seconds !!!ByRef!!! - An integer of seconds
234
/ $what (String) - What shall be extracted ("hours"/"minutes"/"days"/"weeks"/"years")
237
/********************************************************************************************************/
239
function extract_from_seconds(&$seconds,$what) {
241
case "minutes": case "minutes": case "minutes": case "i":
242
$value = bcdiv($seconds,60);
243
$seconds = bcmod($seconds,60);
245
case "hours": case "H": case "h": case "hour":
246
$value = bcdiv($seconds,3600);
247
$seconds = bcmod($seconds,3600);
249
case "days": case "D": case "d": case "day":
250
$value = bcdiv($seconds,3600*24);
251
$seconds = bcmod($seconds,3600*24);
253
case "weeks": case "W": case "w": case "week":
254
$value = bcdiv($seconds,3600*24*7);
255
$seconds = bcmod($seconds,3600*24*7);
257
case "years": case "year": case "y": case "Y":
258
$value = bcdiv($seconds,3600*24*365);
259
$seconds = bcmod($seconds,3600*24*365);
270
/****************************************************************************************************
271
/ @private / public reset -
272
/ resets the Object's basetime to the current Systemtime
275
/********************************************************************************************************/
278
$this->DateTimeObject(mktime());
283
/****************************************************************************************************
284
/ @public setMySQLDateTime($timestamp) -
285
/ Sets the Object's base time to the time provided by Timestring
286
/ return Unix timestamp equivilant
287
/ Parameters : $timestring = String of MySQL DateTime (e.g ("20010911091200") or "2001-09-11 09:12:00")
290
/********************************************************************************************************/
292
function setMySQLDateTime($timestring) {
294
$timestring = preg_replace("#\s#","",$timestring);
295
$timestring = preg_replace("#\:#","",$timestring);
296
$timestring = preg_replace("#\.#","",$timestring);
297
$timestring = preg_replace("#\-#","",$timestring);
298
$timestring = preg_replace("#\/#","",$timestring);
299
$year = (6==strlen($timestring)) ? substr($timestring,0,2): substr($timestring,0,4);
300
$month = (6==strlen($timestring)) ? substr($timestring,2,2): substr($timestring,4,2);
301
$day = (6==strlen($timestring)) ? substr($timestring,4,2): substr($timestring,6,2);
302
$hour = (12 > strlen($timestring)) ? 0 : substr($timestring, 8,2);
303
$minute = (12 > strlen($timestring)) ? 0 : substr($timestring, 10,2);
304
$second = (14 > strlen($timestring)) ? 0 : substr($timestring, 12,2);
306
$this->SimpleDateTimeObject(mktime($hour,$minute,$second,$month, $day,$year));
307
return $this->getTimestamp();
312
/****************************************************************************************************
313
/ @public getMySQLDateTime() -
314
/ Returns the Object's base time in preset MySQLFormat "YmdHis"
318
/********************************************************************************************************/
319
function getMySQLDateTime(){
320
return date("YmdHis",$this->basetime);
324
/****************************************************************************************************
325
/ @public getTimestamp() -
326
/ Returns the Object's base time as Unix TimeStamp
330
/********************************************************************************************************/
331
function getTimestamp() {
332
return $this->basetime;
336
/****************************************************************************************************
338
/ Returns the Day of the Object's base time
339
/ Paramters optional $format - specify the format of the returned value
340
/ according to the allowed Format Strings of
341
/ PHP 's own date command
342
/ optional $timestamp - specify a time of which a part shall be returned
343
/ if not specified the Objects BaseTime will be used
346
/********************************************************************************************************/
348
function day( $format = "d", $timestamp = 0) {
349
if ($timestamp == 0) { return date ($format,$this->basetime);
350
} else {return date ($format,$timestamp);}
352
/****************************************************************************************************
354
/ Returns the Month of the Object's base time
355
/ Paramters optional $format - specify the format of the returned value
356
/ according to the allowed Format Strings of
357
/ PHP 's own date command
358
/ optional $timestamp - specify a time of which a part shall be returned
359
/ if not specified the Objects BaseTime will be used
362
/********************************************************************************************************/
363
function month ( $format = "m", $timestamp = 0) {
364
if ($timestamp == 0) { return date ($format,$this->basetime);
365
} else {return date ($format,$timestamp);}
367
/****************************************************************************************************
369
/ Returns the Year of the Object's base time
370
/ Paramters optional $format - specify the format of the returned value
371
/ according to the allowed Format Strings of
372
/ PHP 's own date command
373
/ optional $timestamp - specify a time of which a part shall be returned
374
/ if not specified the Objects BaseTime will be used
377
/********************************************************************************************************/
378
function year ( $format = "Y", $timestamp = 0) {
379
if ($timestamp == 0) { return date ($format,$this->basetime);
380
} else {return date ($format,$timestamp);}
382
/****************************************************************************************************
384
/ Returns the hour of the Object's base time
385
/ Paramters optional $format - specify the format of the returned value
386
/ according to the allowed Format Strings of
387
/ PHP 's own date command
388
/ optional $timestamp - specify a time of which a part shall be returned
389
/ if not specified the Objects BaseTime will be used
392
/********************************************************************************************************/
393
function hour ( $format = "H", $timestamp = 0) {
394
if ($timestamp == 0) { return date ($format,$this->basetime);
395
} else {return date ($format,$timestamp);}
397
/****************************************************************************************************
399
/ Returns the Minutes of the Object's base time
400
/ Paramters optional $format - specify the format of the returned value
401
/ according to the allowed Format Strings of
402
/ PHP 's own date command
403
/ optional $timestamp - specify a time of which a part shall be returned
404
/ if not specified the Objects BaseTime will be used
407
/********************************************************************************************************/
408
function minute ( $format = "i", $timestamp = 0) {
409
if ($timestamp == 0) { return date ($format,$this->basetime);
410
} else {return date ($format,$timestamp);}
412
/****************************************************************************************************
414
/ Returns the Seconds of the Object's base time
415
/ Paramters optional $format - specify the format of the returned value
416
/ according to the allowed Format Strings of
417
/ PHP 's own date command
418
/ optional $timestamp - specify a time of which a part shall be returned
419
/ if not specified the Objects BaseTime will be used
422
/********************************************************************************************************/
423
function second ( $format = "s", $timestamp = 0) {
424
if ($timestamp == 0) { return date ($format,$this->basetime);
425
} else {return date ($format,$timestamp);}
427
/****************************************************************************************************
428
/ @public getString -
429
/ Returns the Formated Time of the Object's base time
430
/ Paramters optional $format - specify the format of the returned value
431
/ according to the allowed Format Strings of
432
/ PHP 's own date command
433
/ optional $timestamp - specify a time of which a part shall be returned
434
/ if not specified the Objects BaseTime will be used
437
/********************************************************************************************************/
439
function getString ( $format = "M, dS Y h:i a", $timestamp = 0) {
440
if ($timestamp == 0) { return date ($format,$this->basetime);
441
} else {return date ($format,$timestamp);}
446
/****************************************************************************************************
448
/ - returns a string containing a HTML DropDown Field (SELECT) based on the current basetime
450
/ $name - Name of the HTML-Select Tag
451
/ optional $param - Array of Paramaters configuring the Select Field
452
['formatshow'] - Specify the way the entries will be showed
453
/ ['format'] - Specify the kind of Field to be produced (months / years / days ..
455
['start'] - Specify a start value
456
From where will be run?
457
/ ['run'] - Specify the number of entries to be produced
458
/ How far will be run?/
459
/ ['style'] - Specify a CSS - Class to be used
460
/ ['selectsize'] - Specify the Size of the Select Tag ( 1 Row or multirow)
461
/ ['selecteddate'] - preselected Date
463
/***************************************************************************************************/
464
function dropdown ($name, $param=array()) {
465
$start = (isset($param['start']) && ! empty($param['start'])) ? $param['start'] : $this->stdParamAdd['start'];
466
$run = (isset($param['run']) && ! empty($param['run'])) ? $param['run'] : $this->stdParamAdd['run'];
467
$format = (isset($param['format']) && ! empty($param['format'])) ? $param['format'] : "d";
468
$style = (isset($param['style']) && ! empty($param['style'])) ? $param['style'] : "";
469
$selectsize = (isset($param['selectsize']) && ! empty($param['selectsize'])) ? $param['selectsize'] : 1;
471
$selecteddate = (isset($param['selecteddate']) && ! empty($param['selecteddate'])) ? $param['selecteddate'] : $this->basetime;
473
$base_day = date ("d",$this->basetime);
474
$base_month = date ("m",$this->basetime);
475
$base_year = date ("Y",$this->basetime);
476
$base_hour = date ("H",$this->basetime);
477
$base_minute = date ("i",$this->basetime);
478
$base_second = date ("s",$this->basetime);
482
case "months": case "month": case "m":
483
$frm = "m"; $field = &$base_month; $base_day=1; // As there are months which don't have 29th,30th,31st
485
case "years": case "year": case "y": $base_day=1; // As there are months which don't have 29th,30th,31st
486
$frm = "Y"; $field = &$base_year;
488
case "hours": case "hour": case "h":
489
$frm = "H"; $field = &$base_hour;
491
case "minutes": case "minute": case "i":
492
$frm = "i"; $field = &$base_minute;
494
case "seconds": case "second": case "s":
495
$frm = "s"; $field = &$base_second;
497
case "days": case "day": case "d": default:
498
$frm = "d"; $field = &$base_day;
503
$formatshow = (isset($param['formatshow']) && ! empty($param['formatshow'])) ? $param['formatshow'] : $frm;
505
$start = date($frm, $this->basetime) + $start;
506
$selected = date($frm, $selecteddate);
508
for($field=$start; $field<=$start+$run; $field++)
510
$key = date ($frm,mktime ( $base_hour, $base_minute, $base_second, $base_month, $base_day, $base_year));
511
$value = date ($formatshow,mktime ( $base_hour, $base_minute, $base_second, $base_month, $base_day, $base_year));
512
$outputString .= ($key == $selected) ? ' <option value="'.$key.'" selected="selected">'.$value."</option>\n" : ' <option value="'.$key.'">'.$value."</option>\n";
514
return "<select name=\"".$name."\" class=\"$style\" size=\"$selectsize\">\n".$outputString."</select>\n";
518
/****************************************************************************************************
520
/ - returns a string containing a HTML DropDown Field (SELECT) based on the current basetime
522
/ $name - Name of the HTML-Select Tag
523
/ optional $param - Array of Paramaters configuring the Select Field
524
/ ['format'] - Specify the format of the Labels in the Selectbox
525
['keyformat'] - Specify the format of the value in the option tags
526
['running'] - Specify iteration / What will be run ? (e.g. "hours", "years")
527
['start'] - Specify a start value
528
From where will be run?
529
/ ['run'] - Specify the number of entries to be produced
530
/ How far will be run?/
531
['steps'] - Specify the distance between the entries in X format
532
/ ['style'] - Specify a CSS - Class to be used
533
/ ['selectsize'] - Specify the Size of the Select Tag ( 1 Row or multirow)
534
/ ['selecteddate'] - preselected Date
536
/***************************************************************************************************/
537
function datelist ( $name, $param = array()) {
538
$base_day = date ("d",$this->basetime);
539
$base_month = date ("m",$this->basetime);
540
$base_year = date ("Y",$this->basetime);
541
$base_hour = date ("H",$this->basetime);
542
$base_minute = date ("i",$this->basetime);
543
$base_second = date ("s",$this->basetime);
545
$start = (isset($param['start']) && ! empty($param['start'])) ? $param['start'] : $this->stdParamAdd['start'];
546
$run = (isset($param['run']) && ! empty($param['run'])) ? $param['run'] : $this->stdParamAdd['run'];
547
$steps = (isset($param['steps']) && ! empty($param['steps'])) ? $param['steps'] : $this->stdParamAdd['steps'];
548
$running = (isset($param['running']) && ! empty($param['running'])) ? $param['running'] : $this->stdParamAdd['running'];
549
$format = (isset($param['format']) && ! empty($param['format'])) ? $param['format'] : $this->stdParamAdd['format'];
550
$keyformat = (isset($param['keyformat']) && ! empty($param['keyformat'])) ? $param['keyformat'] : $this->stdParamAdd['keyformat'];
551
$style = (isset($param['style']) && ! empty($param['style'])) ? $param['style'] : "";
552
$selectsize = (isset($param['selectsize']) && ! empty($param['selectsize'])) ? $param['selectsize'] : 1;
553
$selecteddate = (isset($param['selecteddate']) && ! empty($param['selecteddate'])) ? $param['selecteddate'] : $this->basetime;
555
$steps = ($steps<1) ? 1 : $steps;
557
case "days": case "day": case "d":
558
$field = &$base_day; $frm = "d";
560
case "months": case "month": case "m":
561
$field = &$base_month; $frm = "m";
563
case "years": case "year": case "y":
564
$field = &$base_year; $frm = "Y";
566
case "hours": case "hour": case "h":
567
$field = &$base_hour; $frm = "H";
569
case "minutes": case "minute": case "i":
570
$field = &$base_minute; $frm = "i";
572
case "seconds": case "second": case "s":
573
$field = &$base_second; $frm = "s";
576
$start = date($frm, $this->basetime) + $start;
577
$selected = date($keyformat, $selecteddate);
579
for($field=$start; $field<=$start+$run; $field=$field+$steps)
581
if ("" == $keyformat) {
582
$key = mktime ( $base_hour, $base_minute, $base_second, $base_month, $base_day, $base_year);
583
} else { $key = date($keyformat,mktime ( $base_hour, $base_minute, $base_second, $base_month, $base_day, $base_year)); }
584
$value = date($format,mktime ( $base_hour, $base_minute, $base_second, $base_month, $base_day, $base_year));
586
$outputString .= ($key==$selected)? ' <option value="'.$key.'" selected="selected">'.$value."</option>\n" : ' <option value="'.$key.'">'.$value."</option>\n";
589
return "<select name=\"".$name."\" class=\"$style\" size=\"$selectsize\">\n".$outputString."</select>\n";
595
if (! function_exists('bcdiv')) {
596
function bcdiv($divident, $divisor) // Ganzzahldivision - ERsatz f�r bcdiv
598
return floor($divident/$divisor);
601
if (! function_exists('bcmod')) {
602
function bcmod($divident, $divisor) // Ganzzahldivisionsmodulo - Eratz f�r bcmod
604
return $divident - ($divisor*bcdiv($divident,$divisor));