Date Class


Cet ensemble se compose de deux classes destinées à la réalisation des calculs avec des dates et des heures.
Une classe de manipulation de dates et un autre pour manipuler les data spans.

Les classes permettent d'ajouter ou de soustraire des périodes de dates et de calculer la différence entre deux dates pour les différentes périodes. Les périodes ans, trimestres, mois, semaines, jours de semaine, heures, minutes, secondes.

10 162  vues
Compatibilité du code
PHP 7
  code classé dans   Classes
  code source classé dans   Classes
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
                    
<?php
/*------------------------------*/
/*
Titre : Date Class

Auteur : freemh
Date édition : 06 Jan 2009
*/
/*------------------------------*/
//===========================================================================
// DateClass
//
// Author: Steve Powell info@scrimpnet.com
// Licensing: GNU Lesser General Public License
// Date: Copyright (c) 2004 scrimpnet.com
//
// Please feel free to email me with any questions, suggestions, or problems.
//
// Public member functions. See member function comments for more complete
// documentation.
//
// SetDate() DateClass set class to a specific date/time
// DatePart() mixed return specific part of the date class
// Add() adjust date by months, days, years, hours, minutes, seconds
// Year() integer return class year
// Month() integer return class month (1-12)
// Day() integer return class day of month (1-31)
// Hours() integer return class hour (0-23)
// Minutes() integer return class minutes (0-59)
// Seconds() integer return class seconds (0-59)
// TimeStamp() integer return PHP base timestamp for class
// BOW() DateClass return first day of week in new DateClass
// EOW() DateClass return last day of week in new DateClass
// BOM() DateClass return first day of month in new DateClass
// EOM() DateClass return last day of month in new DateClass
// Quarter() Integer return quarter (1-4) for class
// BOQ() DateClass return beginning of quarter in new DateClass
// EOQ() DateClass return end of quarter in new DateClass
// BOY() DateClass return beginning of year in new DateClass
// EOY() DateClass return end of year in new DateClass
// ToString() DateClass return a formatted date/time string
// UTC() DateClass return UTC date/time in new DateClass
//
// Most functions take an optional <$dateTime> parameter. If this parameter is


// used it will adjust the class value to <$dateTime> before performing the
// requested action. <$dateTime> can be:
// string 14-JAN-2004 any format that can be parsed by PHP.strtotime()
// 5/15/2004 using the GNU date syntax. These are only
// 2004-03-12 17:35 examples
// integer 1072879687 PHP timestamp
// object (DateClass) existing date class
//
// CUSTOMIZATION HINTS/IDEAS
// (1) The _parseDate() function is responsible for taking input and converting


// it into a PHP timestamp value. Modify this routine to allow class to
// be able to handle more date formats
// (2) Expand class to have better UTC or timezone awareness
// (3) Allow class to handle years that begin on different months
// i.e. Fiscal year starting 4/1/xxxx or 10/1/xxxx
//
// NOTES
// (1) This source code contains many functions you may not need and it
// contains thorough comments. You are free to remove any you
// don't need but you may not remove the licensing or copyright information.
//
// LICENSING
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//=============================================================================


class DateClass
{
//----------------------------
// private member variables
//----------------------------
var $_Date; //base timestamp value for this class
var $_datePart; // array of returned by getdate($_Date)
var $_format; // last format passed to ToString()

//========================================================
// constructor
//========================================================
function DateClass($dateTime="")
{
$this->_format = "Y-m-d H:i:s";
if (!$dateTime)
$this->SetDate(time());
else
$this->SetDate($dateTime);
} //constructor()

//========================================================
//$this->SetDate
//
// Take <$dateTime> and attempt to convert it into a
// timestamp variable. If a string then string must
// be in standard format. If an integer it must be
// a valid timestamp variable. If nothing is passed
// defaults to current time.
//
// NOTE: the entire class depends on parsing $dateTime
// correctly. This is the only place where the
// private timestamp $_Date is directly modified.
//========================================================
function SetDate($dateTime="")
{
$oldStamp = $this->TimeStamp();

//---------------------------------------------------
// try to convert $dateTime to a time stamp
//---------------------------------------------------
if ($dateTime=="")
$this->_Date=time();
else
$this->_Date = $this->_parseDate($dateTime);

//---------------------------------------------------
// parse out date/time components if new timestamp
// is different than previous timestamp. This is
// done only once per value so that repeated calls
// to member functions do no have to repeatedly
// reparse the timestamp
//---------------------------------------------------
if ($oldStamp != $this->TimeStamp())
{
$this->_datePart = getdate($this->TimeStamp());
}
return $this;
} //SetDate()

//========================================================
// _parseDate (private)
//
// Convert $dateTime into a timestamp.
//
// Return: timestamp value of <$dateTime>
//
// NOTE: This is the only place where a date is parsed
// into a timestamp. Modify this routine to
// accept other types of date/time formats
//========================================================
function _parseDate($dateTime="")
{
$ts = ; // timestamp of parsed date
switch(gettype($dateTime))
{
case "string": // <== modify to accomodate more date formats
$ts=strtotime($dateTime);
break;
case "integer":
$ts=$dateTime;
break;
case "object":
if (get_class($dateTime) == "dateclass")
{
$ts = $dateTime->TimeStamp();
}
} //switch
return $ts;
} //_parseDate()

//=========================================================
// DatePart
//
// Returns specific part of current date/time of class.
// If $dateTime is passed then $dateTime replaces value
// of class.
//
// $datePart must conform to valid parameters of getdate()
// seconds mday year yday
// minutes wday month
// hours weekday mon timestamp
//=========================================================
function DatePart($datePart="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if ($datePart=="timestamp") $datePart = ;
return $this->_datePart[$datePart];
} //DatePart()

//=========================================================
// Retrieve parts of date structure
//=========================================================
function Year($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("year");
}
function Month($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("mon");
}
function Day($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("mday");
}
function Hours($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("hours");
}
function Minutes($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("minutes");
}
function Seconds($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("seconds");
}

//===========================================================
// Add()
//
// Adjusts <$adjustPart> of class date by <$adustValue>.
//
// Return a reference to this class that contains adjusted
// timestamp.
//
// <$datePart> string part of date to adjust. The parameter
// can use any class defined variables
// and most of corresponding keys from
// the date() and getdate() functions.
//
// "year" | "years" | "y"
// "months" | "month" | "mon"
// "days" | "day" | "mday" | "d"
// "hours" | "hour" | "g" |
// "minutes" | "minute" | "i"
// "seconds" | "second" | "s"
// <$adjustValue> int Amount to adjust date by. Use negative
// to move backwards in time.
//===========================================================
function Add($datePart,$adjustValue,$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if (!is_int($adjustValue)) $adjustValue = ;

$year = $this->Year();
$month = $this->Month();
$day = $this->Day();
$hour = $this->Hours();
$min = $this->Minutes();
$sec = $this->Seconds();

switch(strtolower($datePart))
{
case "months":
case "month": // month
case "mon":
$this->SetDate(mktime($hour,$min,$sec,$month+$adjustValue,$day,
$year));
break;
case "day": // day
case "days":
case "d":
case "mday":
$this->SetDate(mktime($hour,$min,$sec,$month,$day+$adjustValue,
$year));
break;
case "year": // year
case "years":
case "y":
$this->SetDate(mktime($hour,$min,$sec,$month,$day, $year+
$adjustValue));
break;
case "hours": // hour
case "hour":
case "g": case "G": case "H": case "h":
$this->SetDate(mktime($hour+$adjustValue,$min,$sec,$month,$day,
$year));
break;
case "minutes": // minute
case "minute":
case "i":
$this->SetDate(mktime($hour,$min+$adjustValue,$sec,$month,$day,
$year));
break;
case "seconds": // seconds
case "second":
case "s":
$this->SetDate(mktime($hour,$min,$sec+$adjustValue,$month,$day,
$year));
break;
} //switch
return $this;
} //Add()

//===========================================================
// TimeStamp()
//
// Return timestamp value this class represents.
//
// NOTE: This is the only place the class reads _Date directly
//===========================================================
function TimeStamp($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
return $this->_Date;
} //TimeStamp()

//===========================================================
// BOW() - Beginning of Week
//
// Return date for first day of week. If class
// has time component that remains the same except it is
// now the first day of the week
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOW($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

$dt = new DateClass($this->TimeStamp());
$wday = $dt->DatePart("wday");
$wday = $wday*-1;
$dt->Add("mday",$wday);
return $dt;
} //BOW()

//===========================================================
// EOW() - End of Week
//
// Return date for last day of the week. If class
// has time component that remains the same except it is
// now the last day of the week
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOW($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());
return $dt->Add("day",6-$dt->DatePart("wday"));
}

//===========================================================
// BOM() - Beginning of Month
//
// Return first day of month for the class date. If class
// has time component that remains the same except it is
// now the first day of the month
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOM($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());

$dt->Add("day",-1*($dt->Day()));
return $dt;
} //BOM()

//===========================================================
// EOM() - End of Month
//
// Return last day of month for the class date. If class
// has time component that remains the same except it is
// now the last day of the month
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOM($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());
$dt->Add("month",1);
$dtBOM=$dt->BOM();
$dtBOM->Add("day",-1);
return $dtBOM;
} //EOM()

//===========================================================
// BOY() - Beginning of Year
//
// Return first day of year. If class
// has time component that remains the same except it is
// now the first day of the year
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOY($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

$dt = new DateClass($this->TimeStamp());
$dt->Add("month",-1*($dt->Month()-1));
return $dt->BOM();
} //BOY()

//===========================================================
// EOY() - End of Year
//
// Return last day of year. If class
// has time component that remains the same except it is
// now the last day of the year
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOY($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
$dt = new DateClass($this->TimeStamp());
$dt->Add("year",1);
$dtEOY = $dt->BOY();
$dtEOY->Add("day",-1);
return $dtEOY;
} //EOY()

//===========================================================
// Quarter()
//
// Return the quarter (1-4) for current value of the class
//===========================================================
function Quarter($qDate="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
switch($this->Month())
{
case 1: case 2: case 3: return 1;break;
case 4: case 5: case 6: return 2; break;
case 7: case 8: case 9: return 3; break;
case 10: case 11: case 12: return 4;break;
}
} //Quarter()

//===========================================================
// BOQ() - Beginning of Quarter
//
// Return first day of <$quarter> or the quarter of the current
// value of the class. If class
// has time component that remains the same except it is
// now the first day of the quarter.
//
// <$quarter> integer (1-4)
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOQ($quarter=,$dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
if (!$quarter) $quarter = $this->Quarter();
switch($quarter)
{
case 1: $month = 1;break;
case 2: $month = 4; break;
case 3: $month = 7; break;
case 4: $month = 10;break;
}
$dt = new DateClass(mktime($this->Hours(),$this->Minutes(),$this->
Seconds(),$month,1,$this->Year()));
return $dt;
} //BOQ()

//===========================================================
// EOQ() - End of Quarter
//
// Return last day of <$quarter> or the quarter of the current
// value of the class. If class
// has time component that remains the same except it is
// now the last day of the quarter.
//
// <$quarter> integer (1-4)
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOQ($quarter=,$dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

if (!$quarter) $quarter = $this->Quarter();
$month = $quarter*3;
$dt = new DateClass(mktime($this->Hours(),$this->Minutes(),$this->
Seconds(),$month,1,$this->Year()));
$month = $dt->EOM();
return $month;
} //EOQ()

//===========================================================
// ToString()
//
// Return a formated string of the current date. Use
// <$format> if provided otherwise use
// the format last passed to this function or use default.
//
// You can pass the following for commonly predefined <$format> types:
// RFC822 Mon, 17 May 2004 20:55:42 -0400
// RSS_1.0 2004-05-17T20:55:42-0400
// HTTP Mon, 17 May 2004 20:55:42 GMT
//
// Note: <$format> must be compatible with the PHP.date() function
//===========================================================
function ToString($format="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if($format!="") $this->_format = $format;
switch (strtoupper($format))
{
case "RFC822": return date("r",$this->TimeStamp());
case "RSS_1.0": case "dc:date":
return date("Y-m-d\TH:i:sO",$this->TimeStamp());
case "HTTP": case "RFC1123":
return date('D, d M Y H:i:s \G\M\T',$this->TimeStamp());
}
return date($this->_format,$this->TimeStamp());
} //ToString()

//===========================================================
// UTC()
//
// Return UTC equivelent time for class date value.
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//============================================================
function UTC($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
$dt = date($this);
$dt->Add("minutes",date("Z",$this->TimeStamp()));
return $dt;
} // UTC()

} //class DateClass


//===========================================================================
// DateSpan Class
//
// Author: Steve Powell info@scrimpnet.com
// Licensing: GNU Lesser General Public License
// Date: (c) 2004 ScrimpNet.com
//
// Please feel free to email me with any questions, suggestions, or problems.
//
// This class determines the span of time between two different date/times.
//
// Functions return positive value when StartDate < StopDate. Functions
// return negative values when StartDate > StopDate.
//
// Generally date parameters can take the form of:
// string "3/14/2004" any GNC compatible strtotime() format
// integer 173218828 PHP time stamp
// object object DateClass
//
// Prerequisites: requires DateClass
//
// Public member functions. See member function comments for more complete
// complete descriptions:
// DateSpanClass() constructor
//
// StartDate (property) return DateClass of starting date in span
// StopDate (property) return DateClass of stopping date in span
//
// Years() return number of years covered in span
// Quarters() return number of quarters covered in span
// Months() return number of months covered in span
// Weeks() return number of weeks covered in span
// WeekDays() return number of week days (M-F) in span
// Days() return number of days covered in span
// Hours() return number of hours covered in span
// Minutes() return number of minutes covered in span
// Seconds() return number of seconds covered in span
// Span() return number of periods between two dates
// ToString() return formatted string representing span
// TimeStamp() return PHP timestamp for span (same of Seconds())
//
// NOTES:
// (1) This source code contains many functions you may not need and it
// contains thorough comments. You are free to remove any you
// don't need but you may not remove the licensing or copyright information.
// (2) For performance reasons, multiple calls to the class with the same
// span-start/span-stop dates (and/or times) are not recalculated.
//
// CUSTOMIZATION IDEAS
// (1) Expand class to be able to handle UTC and spans between time zones
// (2) Add capability to handle fiscal years that do not correlate
// to calendar years. i.e. years that begin on 4/1 or 10/1 etc.
//
// LICENSING
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//===========================================================================
class DateSpanClass
{
//------------------------------------------------------
// public member variables (READ ONLY)
//------------------------------------------------------
var $StartDate;
var $StopDate;

//-------------------------------------------------------
// private member variables
//-------------------------------------------------------
var $_months;
var $_days;
var $_weekdays;
var $_hours;
var $_minutes;
var $_seconds;
var $_weeks;
var $_quarters;
var $_format;
var $_years;
var $_spandir; // 1 = positive span, -1 = negative span

//=========================================================
// constructor()
//
// Normally class is instantiated without any parameters.
// Span dates are usually supplied via Span() or property
// accessor functions.
//=========================================================
function DateSpanClass($dateStart="",$dateStop="")
{
$this->_reset();
if (!$dateStart) $dateStart = new DateClass();
if (!$dateStop) $dateStop = new DateClass();
$this->Span("seconds",$dateStart,$dateStop);
} //constructor()

//==========================================================
// _reset (private)
//
// Set class member variables to known state
//==========================================================
function _reset()
{
$this->_format = "m-d-y h:i:s";
$this->_years=;
$this->_months=;
$this->_days=;
$this->_hours=;
$this->_minutes=;
$this->_seconds=;
$this->_weeks=;
$this->_quarters=;
$this->_weekdays=;
$this->_spandir=1;
} //_reset()

//==========================================================
// property accessor functions
//
// optional parameters will calculate
// span between <$dateStart> and <$dateStop>. The span of the
// class will change based on these parameters. Existing
// class values are used if parameters
// are not set.
//
// $dateX parameter can be:
// string "4/13/2004" or any other GNC compatible format
// integer PHP timestamp
// object DateClass
//==========================================================
function Years($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_years==) $this->Span("years",
$dateStart,$dateStop);
return $this->_years;
}
function Months($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_months==) $this->Span("months",
$dateStart,$dateStop);
return $this->_months;
}
function WeekDays($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_weekdays==) $this->Span(
"weekdays",$dateStart,$dateStop);
return $this->_weekdays;
}
function Days($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_days==) $this->Span("days",
$dateStart,$dateStop);
return $this->_days;
}
function Weeks($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_weeks==) $this->Span("weeks",
$dateStart,$dateStop);
return $this->_weeks;
}
function Quarters($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_quarters==) $this->Span(
"quarters",$dateStart,$dateStop);
return $this->_quarters;
}
function Hours($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_hours==) $this->Span("hours",
$dateStart,$dateStop);
return $this->_seconds();
}
function Minutes($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_minutes==) $this->Span("minutes",
$dateStart,$dateStop);
return $this->_minutes();
}
function Seconds($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_seconds==) $this->Span("seconds",
$dateStart,$dateStop);
return $this->_seconds;
}

//=========================================================
// Span()
//
// Calculate span between two dates. Returned value is
// determined by <$datePart>. Numbers could be integer
// or float depending on span result.
//
// <$datePart> string part of date to adjust. The parameter
// can use any the class defined variables
// and most of corresponding keys from
// the date() and getdate() functions.
// "year" | "years" | "y"
// "quarters" | "q"
// "months" | "month" | "mon"
// "weeks" | "w"
// "days" | "day" | "mday" | "d"
// "hours" | "hour" | "g" |
// "minutes" | "minute" | "i"
// "seconds" | "second" | "s"
//
// <$dateStart>,<$dateStop> begin/end dates for time span
// string "5/23/2004" formated GNC date
// integer 393922923 timestamp
// object DateClass
//=========================================================
function Span($period,$dateStart="",$dateStop="")
{
//--------------------------------------------------
// create new span if parameters provided
//--------------------------------------------------
if ($dateStart)
$this->StartDate = new DateClass($dateStart);
if ($dateStop)
$this->StopDate = new DateClass($dateStop);

//--------------------------------------------------
// check to see if we are going to be using a
// negative span (stop date is before start date)
//--------------------------------------------------
$this->_spandir = 1;
if ($this->StartDate->TimeStamp() > $this->StopDate->TimeStamp())
{
$this->_spandir = -1;
}

//--------------------------------------------------
// calculate fixed length intervals
//--------------------------------------------------
$this->_seconds = $this->StopDate->TimeStamp()-$this->StartDate->
TimeStamp();
$this->_minutes = $this->_seconds/60;
$this->_hours = $this->_minutes/60;
$this->_days = $this->_hours/24;
$this->_years = $this->_days/365.25;

//--------------------------------------------------
// return user requested period
//--------------------------------------------------
switch(strtolower($period))
{
case "years": case "year": case "y":
return $this->_years;break;
case "quarters": case "q":
return $this->_calcQuarters();break;
case "months": case "mon": case "m": case "month":
return $this->_calcMonths();break;
case "weeks": case "w":
return $this->_calcWeeks();break;
case "weekdays": case "wdays":
return $this->_calcWeekDays();break;
case "days": case "d": case "day":
return $this->_days;break;
case "hours": case "hour": case "h": case "g":
return $this->_hours;break;
case "minutes": case "minute": case "i":
return $this->_minutes;break;
case "seconds": case "second": case "s":
return $this->_seconds;break;
}
return ; // invalid period parameter
} //span()

//========================================================
// _calcQuarters (private)
//
// Return number of quarter boundries crossed in the
// span. This is a private function and should only
// be called from within the class. Use other accessor
// functions to access the information returned from
// here.
//========================================================
function _calcQuarters()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOQ();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOQ();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter =;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("month",3);
}
$this->_quarters = ($counter-1*$this->_spandir);

return $this->_quarters;

} //_calcQuarters()

//===========================================================
// _calcMonths()
//
// Return number of month boundries crossed in the span. This
// is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcMonths()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOM();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOM();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter =;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("month",1);
}
$this->_months = ($counter-1)*$this->_spandir;

return $this->_months;

} //_calcMonths()

//===========================================================
// _calcWeeks()
//
// Return number of week boundries crossed in the span. This
// means the number of Sunday's between span dates.
//
// This is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcWeeks()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOW();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOW();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter=;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("day",7);
}
$this->_weeks = $counter-1;
$this->_weeks *= $this->_spandir;

return ($this->_weeks);

} //_calcWeeks()

//===========================================================
// _calcWeekDays()
//
// Return number of week boundries crossed in the span. This
// means the number of business days between span dates.
//
// This is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcWeekDays()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter=;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
if ($dtStart->DatePart("wday") > && $dtStart->DatePart("wday") < 5)
$counter++;
$dtStart->Add("day",1);
}
$this->_weekdays = $counter;
$this->_weekdays *= $this->_spandir;

return ($this->_weekdays);

} //_calcWeekDays()
//===========================================================
// TimeStamp()
//
// Return timestamp value this class represents.
//
// This class uses the PHP timestamp for all calculations.
//===========================================================
function TimeStamp()
{
return $this->Seconds();
}

//=========================================================
// ToString()
// Return a formated string of the current timestamp.
// Function uses <$format> if provided otherwise it uses
// the format last passed to this function or use $_format.
//
// Note: <$format> must be compatible with the PHP.date() function
//=========================================================
function ToString($format="")
{
if ($format) $this->_format = $format;
return date($this->_format,$this->TimeStamp());
} //ToString()

} //class DateSpan
?>
<?php
/*------------------------------*/
/*
Titre : Date Class

Auteur : freemh
Date édition : 06 Jan 2009
*/
/*------------------------------*/
//===========================================================================
// DateClass
//
// Author: Steve Powell info@scrimpnet.com
// Licensing: GNU Lesser General Public License
// Date: Copyright (c) 2004 scrimpnet.com
//
// Please feel free to email me with any questions, suggestions, or problems.
//
// Public member functions. See member function comments for more complete
// documentation.
//
// SetDate() DateClass set class to a specific date/time
// DatePart() mixed return specific part of the date class
// Add() adjust date by months, days, years, hours, minutes, seconds
// Year() integer return class year
// Month() integer return class month (1-12)
// Day() integer return class day of month (1-31)
// Hours() integer return class hour (0-23)
// Minutes() integer return class minutes (0-59)
// Seconds() integer return class seconds (0-59)
// TimeStamp() integer return PHP base timestamp for class
// BOW() DateClass return first day of week in new DateClass
// EOW() DateClass return last day of week in new DateClass
// BOM() DateClass return first day of month in new DateClass
// EOM() DateClass return last day of month in new DateClass
// Quarter() Integer return quarter (1-4) for class
// BOQ() DateClass return beginning of quarter in new DateClass
// EOQ() DateClass return end of quarter in new DateClass
// BOY() DateClass return beginning of year in new DateClass
// EOY() DateClass return end of year in new DateClass
// ToString() DateClass return a formatted date/time string
// UTC() DateClass return UTC date/time in new DateClass
//
// Most functions take an optional <$dateTime> parameter. If this parameter is


// used it will adjust the class value to <$dateTime> before performing the
// requested action. <$dateTime> can be:
// string 14-JAN-2004 any format that can be parsed by PHP.strtotime()
// 5/15/2004 using the GNU date syntax. These are only
// 2004-03-12 17:35 examples
// integer 1072879687 PHP timestamp
// object (DateClass) existing date class
//
// CUSTOMIZATION HINTS/IDEAS
// (1) The _parseDate() function is responsible for taking input and converting


// it into a PHP timestamp value. Modify this routine to allow class to
// be able to handle more date formats
// (2) Expand class to have better UTC or timezone awareness
// (3) Allow class to handle years that begin on different months
// i.e. Fiscal year starting 4/1/xxxx or 10/1/xxxx
//
// NOTES
// (1) This source code contains many functions you may not need and it
// contains thorough comments. You are free to remove any you
// don't need but you may not remove the licensing or copyright information.
//
// LICENSING
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//=============================================================================


class DateClass
{
//----------------------------
// private member variables
//----------------------------
var $_Date; //base timestamp value for this class
var $_datePart; // array of returned by getdate($_Date)
var $_format; // last format passed to ToString()

//========================================================
// constructor
//========================================================
function DateClass($dateTime="")
{
$this->_format = "Y-m-d H:i:s";
if (!$dateTime)
$this->SetDate(time());
else
$this->SetDate($dateTime);
} //constructor()

//========================================================
//$this->SetDate
//
// Take <$dateTime> and attempt to convert it into a
// timestamp variable. If a string then string must
// be in standard format. If an integer it must be
// a valid timestamp variable. If nothing is passed
// defaults to current time.
//
// NOTE: the entire class depends on parsing $dateTime
// correctly. This is the only place where the
// private timestamp $_Date is directly modified.
//========================================================
function SetDate($dateTime="")
{
$oldStamp = $this->TimeStamp();

//---------------------------------------------------
// try to convert $dateTime to a time stamp
//---------------------------------------------------
if ($dateTime=="")
$this->_Date=time();
else
$this->_Date = $this->_parseDate($dateTime);

//---------------------------------------------------
// parse out date/time components if new timestamp
// is different than previous timestamp. This is
// done only once per value so that repeated calls
// to member functions do no have to repeatedly
// reparse the timestamp
//---------------------------------------------------
if ($oldStamp != $this->TimeStamp())
{
$this->_datePart = getdate($this->TimeStamp());
}
return $this;
} //SetDate()

//========================================================
// _parseDate (private)
//
// Convert $dateTime into a timestamp.
//
// Return: timestamp value of <$dateTime>
//
// NOTE: This is the only place where a date is parsed
// into a timestamp. Modify this routine to
// accept other types of date/time formats
//========================================================
function _parseDate($dateTime="")
{
$ts = ; // timestamp of parsed date
switch(gettype($dateTime))
{
case "string": // <== modify to accomodate more date formats
$ts=strtotime($dateTime);
break;
case "integer":
$ts=$dateTime;
break;
case "object":
if (get_class($dateTime) == "dateclass")
{
$ts = $dateTime->TimeStamp();
}
} //switch
return $ts;
} //_parseDate()

//=========================================================
// DatePart
//
// Returns specific part of current date/time of class.
// If $dateTime is passed then $dateTime replaces value
// of class.
//
// $datePart must conform to valid parameters of getdate()
// seconds mday year yday
// minutes wday month
// hours weekday mon timestamp
//=========================================================
function DatePart($datePart="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if ($datePart=="timestamp") $datePart = ;
return $this->_datePart[$datePart];
} //DatePart()

//=========================================================
// Retrieve parts of date structure
//=========================================================
function Year($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("year");
}
function Month($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("mon");
}
function Day($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("mday");
}
function Hours($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("hours");
}
function Minutes($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("minutes");
}
function Seconds($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
return $this->DatePart("seconds");
}

//===========================================================
// Add()
//
// Adjusts <$adjustPart> of class date by <$adustValue>.
//
// Return a reference to this class that contains adjusted
// timestamp.
//
// <$datePart> string part of date to adjust. The parameter
// can use any class defined variables
// and most of corresponding keys from
// the date() and getdate() functions.
//
// "year" | "years" | "y"
// "months" | "month" | "mon"
// "days" | "day" | "mday" | "d"
// "hours" | "hour" | "g" |
// "minutes" | "minute" | "i"
// "seconds" | "second" | "s"
// <$adjustValue> int Amount to adjust date by. Use negative
// to move backwards in time.
//===========================================================
function Add($datePart,$adjustValue,$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if (!is_int($adjustValue)) $adjustValue = ;

$year = $this->Year();
$month = $this->Month();
$day = $this->Day();
$hour = $this->Hours();
$min = $this->Minutes();
$sec = $this->Seconds();

switch(strtolower($datePart))
{
case "months":
case "month": // month
case "mon":
$this->SetDate(mktime($hour,$min,$sec,$month+$adjustValue,$day,
$year));
break;
case "day": // day
case "days":
case "d":
case "mday":
$this->SetDate(mktime($hour,$min,$sec,$month,$day+$adjustValue,
$year));
break;
case "year": // year
case "years":
case "y":
$this->SetDate(mktime($hour,$min,$sec,$month,$day, $year+
$adjustValue));
break;
case "hours": // hour
case "hour":
case "g": case "G": case "H": case "h":
$this->SetDate(mktime($hour+$adjustValue,$min,$sec,$month,$day,
$year));
break;
case "minutes": // minute
case "minute":
case "i":
$this->SetDate(mktime($hour,$min+$adjustValue,$sec,$month,$day,
$year));
break;
case "seconds": // seconds
case "second":
case "s":
$this->SetDate(mktime($hour,$min,$sec+$adjustValue,$month,$day,
$year));
break;
} //switch
return $this;
} //Add()

//===========================================================
// TimeStamp()
//
// Return timestamp value this class represents.
//
// NOTE: This is the only place the class reads _Date directly
//===========================================================
function TimeStamp($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
return $this->_Date;
} //TimeStamp()

//===========================================================
// BOW() - Beginning of Week
//
// Return date for first day of week. If class
// has time component that remains the same except it is
// now the first day of the week
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOW($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

$dt = new DateClass($this->TimeStamp());
$wday = $dt->DatePart("wday");
$wday = $wday*-1;
$dt->Add("mday",$wday);
return $dt;
} //BOW()

//===========================================================
// EOW() - End of Week
//
// Return date for last day of the week. If class
// has time component that remains the same except it is
// now the last day of the week
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOW($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());
return $dt->Add("day",6-$dt->DatePart("wday"));
}

//===========================================================
// BOM() - Beginning of Month
//
// Return first day of month for the class date. If class
// has time component that remains the same except it is
// now the first day of the month
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOM($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());

$dt->Add("day",-1*($dt->Day()));
return $dt;
} //BOM()

//===========================================================
// EOM() - End of Month
//
// Return last day of month for the class date. If class
// has time component that remains the same except it is
// now the last day of the month
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOM($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
$dt = new DateClass($this->TimeStamp());
$dt->Add("month",1);
$dtBOM=$dt->BOM();
$dtBOM->Add("day",-1);
return $dtBOM;
} //EOM()

//===========================================================
// BOY() - Beginning of Year
//
// Return first day of year. If class
// has time component that remains the same except it is
// now the first day of the year
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOY($dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

$dt = new DateClass($this->TimeStamp());
$dt->Add("month",-1*($dt->Month()-1));
return $dt->BOM();
} //BOY()

//===========================================================
// EOY() - End of Year
//
// Return last day of year. If class
// has time component that remains the same except it is
// now the last day of the year
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOY($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
$dt = new DateClass($this->TimeStamp());
$dt->Add("year",1);
$dtEOY = $dt->BOY();
$dtEOY->Add("day",-1);
return $dtEOY;
} //EOY()

//===========================================================
// Quarter()
//
// Return the quarter (1-4) for current value of the class
//===========================================================
function Quarter($qDate="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
switch($this->Month())
{
case 1: case 2: case 3: return 1;break;
case 4: case 5: case 6: return 2; break;
case 7: case 8: case 9: return 3; break;
case 10: case 11: case 12: return 4;break;
}
} //Quarter()

//===========================================================
// BOQ() - Beginning of Quarter
//
// Return first day of <$quarter> or the quarter of the current
// value of the class. If class
// has time component that remains the same except it is
// now the first day of the quarter.
//
// <$quarter> integer (1-4)
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function BOQ($quarter=,$dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);
if (!$quarter) $quarter = $this->Quarter();
switch($quarter)
{
case 1: $month = 1;break;
case 2: $month = 4; break;
case 3: $month = 7; break;
case 4: $month = 10;break;
}
$dt = new DateClass(mktime($this->Hours(),$this->Minutes(),$this->
Seconds(),$month,1,$this->Year()));
return $dt;
} //BOQ()

//===========================================================
// EOQ() - End of Quarter
//
// Return last day of <$quarter> or the quarter of the current
// value of the class. If class
// has time component that remains the same except it is
// now the last day of the quarter.
//
// <$quarter> integer (1-4)
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//===========================================================
function EOQ($quarter=,$dateTime="")
{
if ($dateTime) $this->SetDate($DateTime);

if (!$quarter) $quarter = $this->Quarter();
$month = $quarter*3;
$dt = new DateClass(mktime($this->Hours(),$this->Minutes(),$this->
Seconds(),$month,1,$this->Year()));
$month = $dt->EOM();
return $month;
} //EOQ()

//===========================================================
// ToString()
//
// Return a formated string of the current date. Use
// <$format> if provided otherwise use
// the format last passed to this function or use default.
//
// You can pass the following for commonly predefined <$format> types:
// RFC822 Mon, 17 May 2004 20:55:42 -0400
// RSS_1.0 2004-05-17T20:55:42-0400
// HTTP Mon, 17 May 2004 20:55:42 GMT
//
// Note: <$format> must be compatible with the PHP.date() function
//===========================================================
function ToString($format="",$dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
if($format!="") $this->_format = $format;
switch (strtoupper($format))
{
case "RFC822": return date("r",$this->TimeStamp());
case "RSS_1.0": case "dc:date":
return date("Y-m-d\TH:i:sO",$this->TimeStamp());
case "HTTP": case "RFC1123":
return date('D, d M Y H:i:s \G\M\T',$this->TimeStamp());
}
return date($this->_format,$this->TimeStamp());
} //ToString()

//===========================================================
// UTC()
//
// Return UTC equivelent time for class date value.
//
// Returned value is in a new DateClass instance and does
// not modify existing class value
//============================================================
function UTC($dateTime="")
{
if ($dateTime) $this->SetDate($dateTime);
$dt = date($this);
$dt->Add("minutes",date("Z",$this->TimeStamp()));
return $dt;
} // UTC()

} //class DateClass


//===========================================================================
// DateSpan Class
//
// Author: Steve Powell info@scrimpnet.com
// Licensing: GNU Lesser General Public License
// Date: (c) 2004 ScrimpNet.com
//
// Please feel free to email me with any questions, suggestions, or problems.
//
// This class determines the span of time between two different date/times.
//
// Functions return positive value when StartDate < StopDate. Functions
// return negative values when StartDate > StopDate.
//
// Generally date parameters can take the form of:
// string "3/14/2004" any GNC compatible strtotime() format
// integer 173218828 PHP time stamp
// object object DateClass
//
// Prerequisites: requires DateClass
//
// Public member functions. See member function comments for more complete
// complete descriptions:
// DateSpanClass() constructor
//
// StartDate (property) return DateClass of starting date in span
// StopDate (property) return DateClass of stopping date in span
//
// Years() return number of years covered in span
// Quarters() return number of quarters covered in span
// Months() return number of months covered in span
// Weeks() return number of weeks covered in span
// WeekDays() return number of week days (M-F) in span
// Days() return number of days covered in span
// Hours() return number of hours covered in span
// Minutes() return number of minutes covered in span
// Seconds() return number of seconds covered in span
// Span() return number of periods between two dates
// ToString() return formatted string representing span
// TimeStamp() return PHP timestamp for span (same of Seconds())
//
// NOTES:
// (1) This source code contains many functions you may not need and it
// contains thorough comments. You are free to remove any you
// don't need but you may not remove the licensing or copyright information.
// (2) For performance reasons, multiple calls to the class with the same
// span-start/span-stop dates (and/or times) are not recalculated.
//
// CUSTOMIZATION IDEAS
// (1) Expand class to be able to handle UTC and spans between time zones
// (2) Add capability to handle fiscal years that do not correlate
// to calendar years. i.e. years that begin on 4/1 or 10/1 etc.
//
// LICENSING
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//===========================================================================
class DateSpanClass
{
//------------------------------------------------------
// public member variables (READ ONLY)
//------------------------------------------------------
var $StartDate;
var $StopDate;

//-------------------------------------------------------
// private member variables
//-------------------------------------------------------
var $_months;
var $_days;
var $_weekdays;
var $_hours;
var $_minutes;
var $_seconds;
var $_weeks;
var $_quarters;
var $_format;
var $_years;
var $_spandir; // 1 = positive span, -1 = negative span

//=========================================================
// constructor()
//
// Normally class is instantiated without any parameters.
// Span dates are usually supplied via Span() or property
// accessor functions.
//=========================================================
function DateSpanClass($dateStart="",$dateStop="")
{
$this->_reset();
if (!$dateStart) $dateStart = new DateClass();
if (!$dateStop) $dateStop = new DateClass();
$this->Span("seconds",$dateStart,$dateStop);
} //constructor()

//==========================================================
// _reset (private)
//
// Set class member variables to known state
//==========================================================
function _reset()
{
$this->_format = "m-d-y h:i:s";
$this->_years=;
$this->_months=;
$this->_days=;
$this->_hours=;
$this->_minutes=;
$this->_seconds=;
$this->_weeks=;
$this->_quarters=;
$this->_weekdays=;
$this->_spandir=1;
} //_reset()

//==========================================================
// property accessor functions
//
// optional parameters will calculate
// span between <$dateStart> and <$dateStop>. The span of the
// class will change based on these parameters. Existing
// class values are used if parameters
// are not set.
//
// $dateX parameter can be:
// string "4/13/2004" or any other GNC compatible format
// integer PHP timestamp
// object DateClass
//==========================================================
function Years($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_years==) $this->Span("years",
$dateStart,$dateStop);
return $this->_years;
}
function Months($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_months==) $this->Span("months",
$dateStart,$dateStop);
return $this->_months;
}
function WeekDays($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_weekdays==) $this->Span(
"weekdays",$dateStart,$dateStop);
return $this->_weekdays;
}
function Days($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_days==) $this->Span("days",
$dateStart,$dateStop);
return $this->_days;
}
function Weeks($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_weeks==) $this->Span("weeks",
$dateStart,$dateStop);
return $this->_weeks;
}
function Quarters($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_quarters==) $this->Span(
"quarters",$dateStart,$dateStop);
return $this->_quarters;
}
function Hours($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_hours==) $this->Span("hours",
$dateStart,$dateStop);
return $this->_seconds();
}
function Minutes($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_minutes==) $this->Span("minutes",
$dateStart,$dateStop);
return $this->_minutes();
}
function Seconds($dateStart="",$dateStop="")
{
if ($dateStart || $dateStop || $this->_seconds==) $this->Span("seconds",
$dateStart,$dateStop);
return $this->_seconds;
}

//=========================================================
// Span()
//
// Calculate span between two dates. Returned value is
// determined by <$datePart>. Numbers could be integer
// or float depending on span result.
//
// <$datePart> string part of date to adjust. The parameter
// can use any the class defined variables
// and most of corresponding keys from
// the date() and getdate() functions.
// "year" | "years" | "y"
// "quarters" | "q"
// "months" | "month" | "mon"
// "weeks" | "w"
// "days" | "day" | "mday" | "d"
// "hours" | "hour" | "g" |
// "minutes" | "minute" | "i"
// "seconds" | "second" | "s"
//
// <$dateStart>,<$dateStop> begin/end dates for time span
// string "5/23/2004" formated GNC date
// integer 393922923 timestamp
// object DateClass
//=========================================================
function Span($period,$dateStart="",$dateStop="")
{
//--------------------------------------------------
// create new span if parameters provided
//--------------------------------------------------
if ($dateStart)
$this->StartDate = new DateClass($dateStart);
if ($dateStop)
$this->StopDate = new DateClass($dateStop);

//--------------------------------------------------
// check to see if we are going to be using a
// negative span (stop date is before start date)
//--------------------------------------------------
$this->_spandir = 1;
if ($this->StartDate->TimeStamp() > $this->StopDate->TimeStamp())
{
$this->_spandir = -1;
}

//--------------------------------------------------
// calculate fixed length intervals
//--------------------------------------------------
$this->_seconds = $this->StopDate->TimeStamp()-$this->StartDate->
TimeStamp();
$this->_minutes = $this->_seconds/60;
$this->_hours = $this->_minutes/60;
$this->_days = $this->_hours/24;
$this->_years = $this->_days/365.25;

//--------------------------------------------------
// return user requested period
//--------------------------------------------------
switch(strtolower($period))
{
case "years": case "year": case "y":
return $this->_years;break;
case "quarters": case "q":
return $this->_calcQuarters();break;
case "months": case "mon": case "m": case "month":
return $this->_calcMonths();break;
case "weeks": case "w":
return $this->_calcWeeks();break;
case "weekdays": case "wdays":
return $this->_calcWeekDays();break;
case "days": case "d": case "day":
return $this->_days;break;
case "hours": case "hour": case "h": case "g":
return $this->_hours;break;
case "minutes": case "minute": case "i":
return $this->_minutes;break;
case "seconds": case "second": case "s":
return $this->_seconds;break;
}
return ; // invalid period parameter
} //span()

//========================================================
// _calcQuarters (private)
//
// Return number of quarter boundries crossed in the
// span. This is a private function and should only
// be called from within the class. Use other accessor
// functions to access the information returned from
// here.
//========================================================
function _calcQuarters()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOQ();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOQ();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter =;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("month",3);
}
$this->_quarters = ($counter-1*$this->_spandir);

return $this->_quarters;

} //_calcQuarters()

//===========================================================
// _calcMonths()
//
// Return number of month boundries crossed in the span. This
// is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcMonths()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOM();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOM();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter =;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("month",1);
}
$this->_months = ($counter-1)*$this->_spandir;

return $this->_months;

} //_calcMonths()

//===========================================================
// _calcWeeks()
//
// Return number of week boundries crossed in the span. This
// means the number of Sunday's between span dates.
//
// This is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcWeeks()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStart = $dtStart->BOW();
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStart = $dtStart->BOW();
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter=;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
$counter++;
$dtStart->Add("day",7);
}
$this->_weeks = $counter-1;
$this->_weeks *= $this->_spandir;

return ($this->_weeks);

} //_calcWeeks()

//===========================================================
// _calcWeekDays()
//
// Return number of week boundries crossed in the span. This
// means the number of business days between span dates.
//
// This is a private function and should only be called from within
// the class. Use other accessor functions to access the
// information returned from this function.
//===========================================================
function _calcWeekDays()
{
if ($this->_spandir==1) // start is before stop (positive span)
{
$dtStart = new DateClass($this->StartDate);
$dtStop = $this->StopDate;
}
else
{
$dtStart = new DateClass($this->StopDate);
$dtStop = $this->StartDate;
}

//--------------------------------------------------
// count times crossing period boundries
//--------------------------------------------------
$counter=;
while ($dtStart->TimeStamp() < $dtStop->TimeStamp())
{
if ($dtStart->DatePart("wday") > && $dtStart->DatePart("wday") < 5)
$counter++;
$dtStart->Add("day",1);
}
$this->_weekdays = $counter;
$this->_weekdays *= $this->_spandir;

return ($this->_weekdays);

} //_calcWeekDays()
//===========================================================
// TimeStamp()
//
// Return timestamp value this class represents.
//
// This class uses the PHP timestamp for all calculations.
//===========================================================
function TimeStamp()
{
return $this->Seconds();
}

//=========================================================
// ToString()
// Return a formated string of the current timestamp.
// Function uses <$format> if provided otherwise it uses
// the format last passed to this function or use $_format.
//
// Note: <$format> must be compatible with the PHP.date() function
//=========================================================
function ToString($format="")
{
if ($format) $this->_format = $format;
return date($this->_format,$this->TimeStamp());
} //ToString()

} //class DateSpan
?>

      Fonctions du code - Doc officielle PHP

   php.net  
Description
Versions PHP
    date
Formate un horodatage Unix
PHP 4, 5, 7 et 8
    getdate
Retourne la date/heure
PHP 4, 5, 7 et 8
    gettype
Retourne le type de la variable
PHP 4, 5, 7 et 8
    get_class
Retourne le nom de la classe d'un objet
PHP 4, 5, 7 et 8
    is_int
Détermine si une variable est de type nombre entier
PHP 4, 5, 7 et 8
    mktime
Retourne le timestamp UNIX d'une date
PHP 4, 5, 7 et 8
    strtolower
Renvoie une chaîne en minuscules
PHP 4, 5, 7 et 8
    strtotime
Transforme un texte anglais en timestamp
PHP 4, 5, 7 et 8
    strtoupper
Renvoie une chaîne en majuscules
PHP 4, 5, 7 et 8
    time
Retourne l'horodatage UNIX actuel
PHP 4, 5, 7 et 8
Minimum 10 mots. Votre commentaire sera visible après validation.


 Autres snippets qui pourraient vous intéresser

ICQ class - PHP Sources

Compatibilité : PHP 4, PHP 5

Classe ICQ simple pour vérifié si un utilisateur est en ligne ou non.

Class INIT - PHP Sources

Compatibilité : PHP 7

La class INIT permet d'initialisé tout site Web. Il sécurise les données envoyées par l'utilisateur (GET, POST, FILE etc.).

* Requêtes exécutées avec Recherche Contextuelle

  Les derniers scripts

PHP 8.5.5

logo PHP
Langue langue us
Date 12 Avril
Taille 32 Mo
Catégorie PHP

PHP 8.4.20

logo PHP
Langue langue us
Date 12 Avril
Taille 30 Mo
Catégorie PHP

Serendipity 2.6.0

logo Serendipity
Langue langue fr
Date 11 Avril
Taille 15 Mo
Catégorie Blogs

Drupal 11.3.6

logo Drupal
Langue langue us
Date 11 Avril
Taille 34 Mo
Catégorie CMS

TYPO3 14.2.0

logo TYPO3
Langue langue fr
Date 10 Avril
Taille 38 Mo
Catégorie CMS

Dolibarr ERP 23.0.1

logo Dolibarr ERP
Langue langue fr
Date 09 Avril
Taille 89 Mo
Catégorie Logiciels
avatar

Freemh

  06 Jan 2009

  SOURCE   Télécharger
10 162 Vues
Compatibilité du code
PHP 7