/[cvs]/nfo/php/libs/net.php.pear/Date/Calc.php
ViewVC logotype

Annotation of /nfo/php/libs/net.php.pear/Date/Calc.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Oct 29 19:11:41 2002 UTC (21 years, 9 months ago) by cvsjoko
Branch: MAIN
CVS Tags: HEAD
+ new pear-libraries

1 cvsjoko 1.1 <?php
2    
3     // The constant telling us what day starts the week. Monday (1) is the
4     // international standard. Redefine this to 0 if you want weeks to
5     // begin on Sunday.
6     define('DATE_CALC_BEGIN_WEEKDAY', 1);
7    
8     /**
9     * Date_Calc is a calendar class used to calculate and
10     * manipulate calendar dates and retrieve dates in a calendar
11     * format. It does not rely on 32-bit system date stamps, so
12     * you can display calendars and compare dates that date
13     * pre 1970 and post 2038.
14     *
15     * This source file is subject to version 2.02 of the PHP license,
16     * that is bundled with this package in the file LICENSE, and is
17     * available at through the world-wide-web at
18     * http://www.php.net/license/2_02.txt.
19     * If you did not receive a copy of the PHP license and are unable to
20     * obtain it through the world-wide-web, please send a note to
21     * license@php.net so we can mail you a copy immediately.
22     *
23     * Copyright (c) 1999, 2000 ispi
24     *
25     * @access public
26     *
27     * @version 1.2.4
28     * @author Monte Ohrt <monte@ispi.net>
29     */
30    
31     class Date_Calc {
32    
33     /**
34     * Returns the current local date. NOTE: This function
35     * retrieves the local date using strftime(), which may
36     * or may not be 32-bit safe on your system.
37     *
38     * @param string the strftime() format to return the date
39     *
40     * @access public
41     *
42     * @return string the current date in specified format
43     */
44    
45     function dateNow($format="%Y%m%d")
46     {
47     return(strftime($format,time()));
48    
49     } // end func dateNow
50    
51     /**
52     * Returns true for valid date, false for invalid date.
53     *
54     * @param string year in format CCYY
55     * @param string month in format MM
56     * @param string day in format DD
57     *
58     * @access public
59     *
60     * @return boolean true/false
61     */
62    
63     function isValidDate($day, $month, $year)
64     {
65    
66     if(empty($year) || empty($month) || empty($day))
67     return false;
68    
69     // must be digits only
70     if(preg_match("/\D/",$year))
71     return false;
72     if(preg_match("/\D/",$month))
73     return false;
74     if(preg_match("/\D/",$day))
75     return false;
76    
77     if($year < 0 || $year > 9999)
78     return false;
79     if($month < 1 || $month > 12)
80     return false;
81     if($day < 1 || $day > 31 || $day > Date_Calc::daysInMonth($month,$year))
82     return false;
83    
84     return true;
85    
86     } // end func isValidDate
87    
88     function isLeapYear($year="")
89     {
90    
91     if(empty($year))
92     $year = Date_Calc::dateNow("%Y");
93    
94     if(strlen($year) != 4)
95     return false;
96    
97     if(preg_match("/\D/",$year))
98     return false;
99    
100     return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
101    
102     } // end func isLeapYear
103    
104     /**
105     * Determines if given date is a future date from now.
106     *
107     * @param string year in format CCYY
108     * @param string month in format MM
109     * @param string day in format DD
110     *
111     * @access public
112     *
113     * @return boolean true/false
114     */
115    
116     function isFutureDate($day,$month,$year)
117     {
118     $this_year = Date_Calc::dateNow("%Y");
119     $this_month = Date_Calc::dateNow("%m");
120     $this_day = Date_Calc::dateNow("%d");
121    
122    
123     if($year > $this_year)
124     return true;
125     elseif($year == $this_year)
126     if($month > $this_month)
127     return true;
128     elseif($month == $this_month)
129     if($day > $this_day)
130     return true;
131    
132     return false;
133    
134     } // end func isFutureDate
135    
136     /**
137     * Determines if given date is a past date from now.
138     *
139     * @param string year in format CCYY
140     * @param string month in format MM
141     * @param string day in format DD
142     *
143     * @access public
144     *
145     * @return boolean true/false
146     */
147    
148     function isPastDate($day,$month,$year)
149     {
150     $this_year = Date_Calc::dateNow("%Y");
151     $this_month = Date_Calc::dateNow("%m");
152     $this_day = Date_Calc::dateNow("%d");
153    
154    
155     if($year < $this_year)
156     return true;
157     elseif($year == $this_year)
158     if($month < $this_month)
159     return true;
160     elseif($month == $this_month)
161     if($day < $this_day)
162     return true;
163    
164     return false;
165    
166     } // end func isPastDate
167    
168     /**
169     * Returns day of week for given date, 0=Sunday
170     *
171     * @param string year in format CCYY, default is current local year
172     * @param string month in format MM, default is current local month
173     * @param string day in format DD, default is current local day
174     *
175     * @access public
176     *
177     * @return int $weekday_number
178     */
179    
180     function dayOfWeek($day="",$month="",$year="")
181     {
182    
183     if(empty($year))
184     $year = Date_Calc::dateNow("%Y");
185     if(empty($month))
186     $month = Date_Calc::dateNow("%m");
187     if(empty($day))
188     $day = Date_Calc::dateNow("%d");
189    
190     if($month > 2)
191     $month -= 2;
192     else
193     {
194     $month += 10;
195     $year--;
196     }
197    
198     $day = ( floor((13 * $month - 1) / 5) +
199     $day + ($year % 100) +
200     floor(($year % 100) / 4) +
201     floor(($year / 100) / 4) - 2 *
202     floor($year / 100) + 77);
203    
204     $weekday_number = (($day - 7 * floor($day / 7)));
205    
206     return $weekday_number;
207    
208     } // end func dayOfWeek
209    
210     /**
211     * Returns week of the year, first Sunday is first day of first week
212     *
213     * @param string day in format DD
214     * @param string month in format MM
215     * @param string year in format CCYY
216     *
217     * @access public
218     *
219     * @return integer $week_number
220     */
221    
222     function weekOfYear($day,$month,$year)
223     {
224     if(empty($year))
225     $year = Date_Calc::dateNow("%Y");
226     if(empty($month))
227     $month = Date_Calc::dateNow("%m");
228     if(empty($day))
229     $day = Date_Calc::dateNow("%d");
230    
231     $week_year = $year - 1501;
232     $week_day = $week_year * 365 + floor($week_year / 4) - 29872 + 1
233     - floor($week_year / 100) + floor(($week_year - 300) / 400);
234    
235     $week_number =
236     ceil((Date_Calc::julianDate($day,$month,$year) + floor(($week_day + 4) % 7)) / 7);
237    
238     return $week_number;
239    
240     } // end func weekOfYear
241    
242     /**
243     * Returns number of days since 31 December of year before given date.
244     *
245     * @param string year in format CCYY, default is current local year
246     * @param string month in format MM, default is current local month
247     * @param string day in format DD, default is current local day
248     *
249     * @access public
250     *
251     * @return int $julian
252     */
253    
254     function julianDate($day="",$month="",$year="")
255     {
256     if(empty($year))
257     $year = Date_Calc::dateNow("%Y");
258     if(empty($month))
259     $month = Date_Calc::dateNow("%m");
260     if(empty($day))
261     $day = Date_Calc::dateNow("%d");
262    
263     $days = array(0,31,59,90,120,151,181,212,243,273,304,334);
264    
265     $julian = ($days[$month - 1] + $day);
266    
267     if($month > 2 && Date_Calc::isLeapYear($year))
268     $julian++;
269    
270     return($julian);
271    
272     } // end func julianDate
273    
274     /**
275     * Returns quarter of the year for given date
276     *
277     * @param string year in format CCYY, default current local year
278     * @param string month in format MM, default current local month
279     * @param string day in format DD, default current local day
280     *
281     * @access public
282     *
283     * @return int $year_quarter
284     */
285    
286     function quarterOfYear($day="",$month="",$year="")
287     {
288     if(empty($year))
289     $year = Date_Calc::dateNow("%Y");
290     if(empty($month))
291     $month = Date_Calc::dateNow("%m");
292     if(empty($day))
293     $day = Date_Calc::dateNow("%d");
294    
295     $year_quarter = (intval(($month - 1) / 3 + 1));
296    
297     return $year_quarter;
298    
299     } // end func quarterOfYear
300    
301     /**
302     * Returns date of begin of next month of given date.
303     *
304     * @param string year in format CCYY, default current local year
305     * @param string month in format MM, default current local month
306     * @param string day in format DD, default current local day
307     * @param string format for returned date
308     *
309     * @access public
310     *
311     * @return string date in given format
312     */
313    
314     function beginOfNextMonth($day="",$month="",$year="",$format="%Y%m%d")
315     {
316     if(empty($year))
317     $year = Date_Calc::dateNow("%Y");
318     if(empty($month))
319     $month = Date_Calc::dateNow("%m");
320     if(empty($day))
321     $day = Date_Calc::dateNow("%d");
322    
323     if($month < 12)
324     {
325     $month++;
326     $day=1;
327     }
328     else
329     {
330     $year++;
331     $month=1;
332     $day=1;
333     }
334    
335     return Date_Calc::dateFormat($day,$month,$year,$format);
336    
337     } // end func beginOfNextMonth
338    
339     /**
340     * Returns date of the last day of next month of given date.
341     *
342     * @param string year in format CCYY, default current local year
343     * @param string month in format MM, default current local month
344     * @param string day in format DD, default current local day
345     * @param string format for returned date
346     *
347     * @access public
348     *
349     * @return string date in given format
350     */
351    
352     function endOfNextMonth($day="",$month="",$year="",$format="%Y%m%d")
353     {
354     if(empty($year))
355     $year = Date_Calc::dateNow("%Y");
356     if(empty($month))
357     $month = Date_Calc::dateNow("%m");
358     if(empty($day))
359     $day = Date_Calc::dateNow("%d");
360    
361    
362     if($month < 12)
363     {
364     $month++;
365     }
366     else
367     {
368     $year++;
369     $month=1;
370     }
371    
372     $day = Date_Calc::daysInMonth($month,$year);
373    
374     return Date_Calc::dateFormat($day,$month,$year,$format);
375    
376     } // end func endOfNextMonth
377    
378     /**
379     * Returns date of the first day of previous month of given date.
380     *
381     * @param string year in format CCYY, default current local year
382     * @param string month in format MM, default current local month
383     * @param string day in format DD, default current local day
384     * @param string format for returned date
385     *
386     * @access public
387     *
388     * @return string date in given format
389     */
390    
391     function beginOfPrevMonth($day="",$month="",$year="",$format="%Y%m%d")
392     {
393     if(empty($year))
394     $year = Date_Calc::dateNow("%Y");
395     if(empty($month))
396     $month = Date_Calc::dateNow("%m");
397     if(empty($day))
398     $day = Date_Calc::dateNow("%d");
399    
400     if($month > 1)
401     {
402     $month--;
403     $day=1;
404     }
405     else
406     {
407     $year--;
408     $month=12;
409     $day=1;
410     }
411    
412     return Date_Calc::dateFormat($day,$month,$year,$format);
413    
414     } // end func beginOfPrevMonth
415    
416     /**
417     * Returns date of the last day of previous month for given date.
418     *
419     * @param string year in format CCYY, default current local year
420     * @param string month in format MM, default current local month
421     * @param string day in format DD, default current local day
422     * @param string format for returned date
423     *
424     * @access public
425     *
426     * @return string date in given format
427     */
428    
429     function endOfPrevMonth($day="",$month="",$year="",$format="%Y%m%d")
430     {
431     if(empty($year))
432     $year = Date_Calc::dateNow("%Y");
433     if(empty($month))
434     $month = Date_Calc::dateNow("%m");
435     if(empty($day))
436     $day = Date_Calc::dateNow("%d");
437    
438     if($month > 1)
439     {
440     $month--;
441     }
442     else
443     {
444     $year--;
445     $month=12;
446     }
447    
448     $day = Date_Calc::daysInMonth($month,$year);
449    
450     return Date_Calc::dateFormat($day,$month,$year,$format);
451    
452     } // end func endOfPrevMonth
453    
454     /**
455     * Returns date of the next weekday of given date,
456     * skipping from Friday to Monday.
457     *
458     * @param string year in format CCYY, default current local year
459     * @param string month in format MM, default current local month
460     * @param string day in format DD, default current local day
461     * @param string format for returned date
462     *
463     * @access public
464     *
465     * @return string date in given format
466     */
467    
468     function nextWeekday($day="",$month="",$year="",$format="%Y%m%d")
469     {
470     if(empty($year))
471     $year = Date_Calc::dateNow("%Y");
472     if(empty($month))
473     $month = Date_Calc::dateNow("%m");
474     if(empty($day))
475     $day = Date_Calc::dateNow("%d");
476    
477     $days = Date_Calc::dateToDays($day,$month,$year);
478    
479     if(Date_Calc::dayOfWeek($day,$month,$year) == 5)
480     $days += 3;
481     elseif(Date_Calc::dayOfWeek($day,$month,$year) == 6)
482     $days += 2;
483     else
484     $days += 1;
485    
486     return(Date_Calc::daysToDate($days,$format));
487    
488     } // end func nextWeekday
489    
490     /**
491     * Returns date of the previous weekday,
492     * skipping from Monday to Friday.
493     *
494     * @param string year in format CCYY, default current local year
495     * @param string month in format MM, default current local month
496     * @param string day in format DD, default current local day
497     * @param string format for returned date
498     *
499     * @access public
500     *
501     * @return string date in given format
502     */
503    
504     function prevWeekday($day="",$month="",$year="",$format="%Y%m%d")
505     {
506     if(empty($year))
507     $year = Date_Calc::dateNow("%Y");
508     if(empty($month))
509     $month = Date_Calc::dateNow("%m");
510     if(empty($day))
511     $day = Date_Calc::dateNow("%d");
512    
513     $days = Date_Calc::dateToDays($day,$month,$year);
514    
515     if(Date_Calc::dayOfWeek($day,$month,$year) == 1)
516     $days -= 3;
517     elseif(Date_Calc::dayOfWeek($day,$month,$year) == 0)
518     $days -= 2;
519     else
520     $days -= 1;
521    
522     return(Date_Calc::daysToDate($days,$format));
523    
524     } // end func prevWeekday
525    
526     /**
527     * Returns date of the next specific day of the week
528     * from the given date.
529     *
530     * @param int day of week, 0=Sunday
531     * @param string year in format CCYY, default current local year
532     * @param string month in format MM, default current local month
533     * @param string day in format DD, default current local day
534     * @param boolean onOrAfter if true and days are same, returns current day
535     * @param string format for returned date
536     *
537     * @access public
538     *
539     * @return string date in given format
540     */
541    
542     function nextDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",$onOrAfter=false)
543     {
544     if(empty($year))
545     $year = Date_Calc::dateNow("%Y");
546     if(empty($month))
547     $month = Date_Calc::dateNow("%m");
548     if(empty($day))
549     $day = Date_Calc::dateNow("%d");
550    
551     $days = Date_Calc::dateToDays($day,$month,$year);
552     $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year);
553    
554     if($curr_weekday == $dow)
555     {
556     if(!$onOrAfter)
557     $days += 7;
558     }
559     elseif($curr_weekday > $dow)
560     $days += 7 - ( $curr_weekday - $dow );
561     else
562     $days += $dow - $curr_weekday;
563    
564     return(Date_Calc::daysToDate($days,$format));
565    
566     } // end func nextDayOfWeek
567    
568     /**
569     * Returns date of the previous specific day of the week
570     * from the given date.
571     *
572     * @param int day of week, 0=Sunday
573     * @param string year in format CCYY, default current local year
574     * @param string month in format MM, default current local month
575     * @param string day in format DD, default current local day
576     * @param boolean onOrBefore if true and days are same, returns current day
577     * @param string format for returned date
578     *
579     * @access public
580     *
581     * @return string date in given format
582     */
583    
584     function prevDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",$onOrBefore=false)
585     {
586     if(empty($year))
587     $year = Date_Calc::dateNow("%Y");
588     if(empty($month))
589     $month = Date_Calc::dateNow("%m");
590     if(empty($day))
591     $day = Date_Calc::dateNow("%d");
592    
593     $days = Date_Calc::dateToDays($day,$month,$year);
594     $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year);
595    
596     if($curr_weekday == $dow)
597     {
598     if(!$onOrBefore)
599     $days -= 7;
600     }
601     elseif($curr_weekday < $dow)
602     $days -= 7 - ( $dow - $curr_weekday );
603     else
604     $days -= $curr_weekday - $dow;
605    
606     return(Date_Calc::daysToDate($days,$format));
607    
608     } // end func prevDayOfWeek
609    
610     /**
611     * Returns date of the next specific day of the week
612     * on or before the given date.
613     *
614     * @param int day of week, 0=Sunday
615     * @param string year in format CCYY, default current local year
616     * @param string month in format MM, default current local month
617     * @param string day in format DD, default current local day
618     * @param string format for returned date
619     *
620     * @access public
621     *
622     * @return string date in given format
623     */
624    
625     function nextDayOfWeekOnOrAfter($dow,$day="",$month="",$year="",$format="%Y%m%d")
626     {
627     return(Date_Calc::nextDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",true));
628     } // end func nextDayOfWeekOnOrAfter
629    
630     /**
631     * Returns date of the previous specific day of the week
632     * on or before the given date.
633     *
634     * @param int day of week, 0=Sunday
635     * @param string year in format CCYY, default current local year
636     * @param string month in format MM, default current local month
637     * @param string day in format DD, default current local day
638     * @param string format for returned date
639     *
640     * @access public
641     *
642     * @return string date in given format
643     */
644    
645     function prevDayOfWeekOnOrBefore($dow,$day="",$month="",$year="",$format="%Y%m%d")
646     {
647     return(Date_Calc::prevDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",true));
648    
649     } // end func prevDayOfWeekOnOrAfter
650    
651     /**
652     * Returns date of day after given date.
653     *
654     * @param string year in format CCYY, default current local year
655     * @param string month in format MM, default current local month
656     * @param string day in format DD, default current local day
657     * @param string format for returned date
658     *
659     * @access public
660     *
661     * @return string date in given format
662     */
663    
664     function nextDay($day="",$month="",$year="",$format="%Y%m%d")
665     {
666     if(empty($year))
667     $year = Date_Calc::dateNow("%Y");
668     if(empty($month))
669     $month = Date_Calc::dateNow("%m");
670     if(empty($day))
671     $day = Date_Calc::dateNow("%d");
672    
673     $days = Date_Calc::dateToDays($day,$month,$year);
674    
675     return(Date_Calc::daysToDate($days + 1,$format));
676    
677     } // end func nextDay
678    
679     /**
680     * Returns date of day before given date.
681     *
682     * @param string year in format CCYY, default current local year
683     * @param string month in format MM, default current local month
684     * @param string day in format DD, default current local day
685     * @param string format for returned date
686     *
687     * @access public
688     *
689     * @return string date in given format
690     */
691    
692     function prevDay($day="",$month="",$year="",$format="%Y%m%d")
693     {
694     if(empty($year))
695     $year = Date_Calc::dateNow("%Y");
696     if(empty($month))
697     $month = Date_Calc::dateNow("%m");
698     if(empty($day))
699     $day = Date_Calc::dateNow("%d");
700    
701     $days = Date_Calc::dateToDays($day,$month,$year);
702    
703     return(Date_Calc::daysToDate($days - 1,$format));
704    
705     } // end func prevDay
706    
707     /**
708     * Sets century for 2 digit year.
709     * 51-99 is 19, else 20
710     *
711     * @param string 2 digit year
712     *
713     * @access public
714     *
715     * @return string 4 digit year
716     */
717    
718     function defaultCentury($year)
719     {
720     if(strlen($year) == 1)
721     $year = "0$year";
722     if($year > 50)
723     return( "19$year" );
724     else
725     return( "20$year" );
726    
727     } // end func defaultCentury
728    
729     /**
730     * Returns number of days between two given dates.
731     *
732     * @param string year in format CCYY
733     * @param string month in format MM
734     * @param string day in format DD
735     * @param string year in format CCYY
736     * @param string month in format MM
737     * @param string day in format DD
738     *
739     * @access public
740     *
741     * @return int absolute number of days between dates,
742     * -1 if there is an error.
743     */
744    
745     function dateDiff($day1,$month1,$year1,$day2,$month2,$year2)
746     {
747     if(!Date_Calc::isValidDate($day1,$month1,$year1))
748     return -1;
749     if(!Date_Calc::isValidDate($day2,$month2,$year2))
750     return -1;
751    
752     return(abs((Date_Calc::dateToDays($day1,$month1,$year1))
753     - (Date_Calc::dateToDays($day2,$month2,$year2))));
754    
755     } // end func dateDiff
756    
757     /**
758     * Find the number of days in the given month.
759     *
760     * @param string month in format MM, default current local month
761     *
762     * @access public
763     *
764     * @return int number of days
765     */
766    
767     function daysInMonth($month="",$year="")
768     {
769     if(empty($year))
770     $year = Date_Calc::dateNow("%Y");
771     if(empty($month))
772     $month = Date_Calc::dateNow("%m");
773    
774     if($month == 2)
775     {
776     if(Date_Calc::isLeapYear($year))
777     return 29;
778     else
779     return 28;
780     }
781     elseif($month == 4 or $month == 6 or $month == 9 or $month == 11)
782     return 30;
783     else
784     return 31;
785    
786     } // end func daysInMonth
787    
788     /**
789     * Returns the number of rows on a calendar month. Useful for
790     * determining the number of rows when displaying a typical
791     * month calendar.
792     *
793     * @param string month in format MM, default current local month
794     * @param string year in format YYCC, default current local year
795     *
796     * @access public
797     *
798     * @return int number of weeks
799     */
800    
801     function weeksInMonth($month="",$year="")
802     {
803     if(empty($year))
804     $year = Date_Calc::dateNow("%Y");
805     if(empty($month))
806     $month = Date_Calc::dateNow("%m");
807    
808     if(DATE_CALC_BEGIN_WEEKDAY == 1)
809     {
810    
811     if(Date_Calc::firstOfMonthWeekday($month,$year) == 0)
812     $first_week_days = 1;
813     else
814     $first_week_days = 7 - (Date_Calc::firstOfMonthWeekday($month,$year) - 1);
815    
816     }
817     else
818     $first_week_days = 7 - Date_Calc::firstOfMonthWeekday($month,$year);
819    
820     return ceil(((Date_Calc::daysInMonth($month,$year) - $first_week_days) / 7) + 1);
821    
822     } // end func weeksInMonth
823    
824     /**
825     * Find the day of the week for the first of the month of given date.
826     *
827     * @param string year in format CCYY, default to current local year
828     * @param string month in format MM, default to current local month
829     *
830     * @access public
831     *
832     * @return int number of weekday for the first day, 0=Sunday
833     */
834    
835     function firstOfMonthWeekday($month="",$year="")
836     {
837     if(empty($year))
838     $year = Date_Calc::dateNow("%Y");
839     if(empty($month))
840     $month = Date_Calc::dateNow("%m");
841    
842     return(Date_Calc::dayOfWeek("01",$month,$year));
843    
844     } // end func firstOfMonthWeekday
845    
846     /**
847     * Return date of first day of month of given date.
848     *
849     * @param string year in format CCYY, default current local year
850     * @param string month in format MM, default current local month
851     * @param string format for returned date
852     *
853     * @access public
854     *
855     * @return string date in given format
856     */
857    
858     function beginOfMonth($month="",$year="",$format="%Y%m%d")
859     {
860     if(empty($year))
861     $year = Date_Calc::dateNow("%Y");
862     if(empty($month))
863     $month = Date_Calc::dateNow("%m");
864    
865     return(Date_Calc::dateFormat("01",$month,$year,$format));
866    
867     } // end of func beginOfMonth
868    
869     /**
870     * Find the month day of the beginning of week for given date,
871     * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
872     *
873     * @param string year in format CCYY, default current local year
874     * @param string month in format MM, default current local month
875     * @param string day in format DD, default current local day
876     * @param string format for returned date
877     *
878     * @access public
879     *
880     * @return string date in given format
881     */
882    
883     function beginOfWeek($day="",$month="",$year="",$format="%Y%m%d")
884     {
885     if(empty($year))
886     $year = Date_Calc::dateNow("%Y");
887     if(empty($month))
888     $month = Date_Calc::dateNow("%m");
889     if(empty($day))
890     $day = Date_Calc::dateNow("%d");
891    
892     $this_weekday = Date_Calc::dayOfWeek($day,$month,$year);
893    
894     if(DATE_CALC_BEGIN_WEEKDAY == 1)
895     {
896     if($this_weekday == 0)
897     $beginOfWeek = Date_Calc::dateToDays($day,$month,$year) - 6;
898     else
899     $beginOfWeek = Date_Calc::dateToDays($day,$month,$year)
900     - $this_weekday + 1;
901     }
902     else
903     $beginOfWeek = (Date_Calc::dateToDays($day,$month,$year)
904     - $this_weekday);
905    
906    
907     /* $beginOfWeek = (Date_Calc::dateToDays($day,$month,$year)
908     - ($this_weekday - DATE_CALC_BEGIN_WEEKDAY)); */
909    
910     return(Date_Calc::daysToDate($beginOfWeek,$format));
911    
912     } // end of func beginOfWeek
913    
914     /**
915     * Find the month day of the end of week for given date,
916     * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday
917     * of following month.)
918     *
919     * @param string year in format CCYY, default current local year
920     * @param string month in format MM, default current local month
921     * @param string day in format DD, default current local day
922     * @param string format for returned date
923     *
924     * @access public
925     *
926     * @return string date in given format
927     */
928    
929     function endOfWeek($day="",$month="",$year="",$format="%Y%m%d")
930     {
931     if(empty($year))
932     $year = Date_Calc::dateNow("%Y");
933     if(empty($month))
934     $month = Date_Calc::dateNow("%m");
935     if(empty($day))
936     $day = Date_Calc::dateNow("%d");
937    
938     $this_weekday = Date_Calc::dayOfWeek($day,$month,$year);
939    
940     $last_dayOfWeek = (Date_Calc::dateToDays($day,$month,$year)
941     + (6 - $this_weekday + DATE_CALC_BEGIN_WEEKDAY));
942    
943     return(Date_Calc::daysToDate($last_dayOfWeek,$format));
944    
945     } // end func endOfWeek
946    
947     /**
948     * Find the month day of the beginning of week after given date,
949     * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
950     *
951     * @param string year in format CCYY, default current local year
952     * @param string month in format MM, default current local month
953     * @param string day in format DD, default current local day
954     * @param string format for returned date
955     *
956     * @access public
957     *
958     * @return string date in given format
959     */
960    
961     function beginOfNextWeek($day="",$month="",$year="",$format="%Y%m%d")
962     {
963     if(empty($year))
964     $year = Date_Calc::dateNow("%Y");
965     if(empty($month))
966     $month = Date_Calc::dateNow("%m");
967     if(empty($day))
968     $day = Date_Calc::dateNow("%d");
969    
970     $date = Date_Calc::daysToDate(Date_Calc::dateToDays($day+7,$month,$year),"%Y%m%d");
971    
972     $next_week_year = substr($date,0,4);
973     $next_week_month = substr($date,4,2);
974     $next_week_day = substr($date,6,2);
975    
976     $this_weekday = Date_Calc::dayOfWeek($next_week_day,$next_week_month,$next_week_year);
977    
978     $beginOfWeek = (Date_Calc::dateToDays($next_week_day,$next_week_month,$next_week_year)
979     - ($this_weekday - DATE_CALC_BEGIN_WEEKDAY));
980    
981     return(Date_Calc::daysToDate($beginOfWeek,$format));
982    
983     } // end func beginOfNextWeek
984    
985     /**
986     * Find the month day of the beginning of week before given date,
987     * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
988     *
989     * @param string year in format CCYY, default current local year
990     * @param string month in format MM, default current local month
991     * @param string day in format DD, default current local day
992     * @param string format for returned date
993     *
994     * @access public
995     *
996     * @return string date in given format
997     */
998    
999     function beginOfPrevWeek($day="",$month="",$year="",$format="%Y%m%d")
1000     {
1001     if(empty($year))
1002     $year = Date_Calc::dateNow("%Y");
1003     if(empty($month))
1004     $month = Date_Calc::dateNow("%m");
1005     if(empty($day))
1006     $day = Date_Calc::dateNow("%d");
1007    
1008     $date = Date_Calc::daysToDate(Date_Calc::dateToDays($day-7,$month,$year),"%Y%m%d");
1009    
1010     $next_week_year = substr($date,0,4);
1011     $next_week_month = substr($date,4,2);
1012     $next_week_day = substr($date,6,2);
1013    
1014     $this_weekday = Date_Calc::dayOfWeek($next_week_day,$next_week_month,$next_week_year);
1015    
1016     $beginOfWeek = (Date_Calc::dateToDays($next_week_day,$next_week_month,$next_week_year)
1017     - ($this_weekday - DATE_CALC_BEGIN_WEEKDAY));
1018    
1019     return(Date_Calc::daysToDate($beginOfWeek,$format));
1020    
1021     } // end func beginOfPrevWeek
1022    
1023     /**
1024     * Return an array with days in week
1025     *
1026     * @param string year in format CCYY, default current local year
1027     * @param string month in format MM, default current local month
1028     * @param string day in format DD, default current local day
1029     * @param string format for returned date
1030     *
1031     * @access public
1032     *
1033     * @return array $week[$weekday]
1034     */
1035    
1036     function getCalendarWeek($day="",$month="",$year="",$format="%Y%m%d")
1037     {
1038     if(empty($year))
1039     $year = Date_Calc::dateNow("%Y");
1040     if(empty($month))
1041     $month = Date_Calc::dateNow("%m");
1042     if(empty($day))
1043     $day = Date_Calc::dateNow("%d");
1044    
1045     $week_array = array();
1046    
1047     // date for the column of week
1048    
1049     $curr_day = Date_Calc::beginOfWeek($day,$month,$year,"%E");
1050    
1051     for($counter=0; $counter <= 6; $counter++)
1052     {
1053     $week_array[$counter] = Date_Calc::daysToDate($curr_day,$format);
1054     $curr_day++;
1055     }
1056    
1057     return $week_array;
1058    
1059     } // end func getCalendarWeek
1060    
1061     /**
1062     * Return a set of arrays to construct a calendar month for
1063     * the given date.
1064     *
1065     * @param string year in format CCYY, default current local year
1066     * @param string month in format MM, default current local month
1067     * @param string format for returned date
1068     *
1069     * @access public
1070     *
1071     * @return array $month[$row][$col]
1072     */
1073    
1074     function getCalendarMonth($month="",$year="",$format="%Y%m%d")
1075     {
1076     if(empty($year))
1077     $year = Date_Calc::dateNow("%Y");
1078     if(empty($month))
1079     $month = Date_Calc::dateNow("%m");
1080    
1081     $month_array = array();
1082    
1083     // date for the first row, first column of calendar month
1084     if(DATE_CALC_BEGIN_WEEKDAY == 1)
1085     {
1086     if(Date_Calc::firstOfMonthWeekday($month,$year) == 0)
1087     $curr_day = Date_Calc::dateToDays("01",$month,$year) - 6;
1088     else
1089     $curr_day = Date_Calc::dateToDays("01",$month,$year)
1090     - Date_Calc::firstOfMonthWeekday($month,$year) + 1;
1091     }
1092     else
1093     $curr_day = (Date_Calc::dateToDays("01",$month,$year)
1094     - Date_Calc::firstOfMonthWeekday($month,$year));
1095    
1096     // number of days in this month
1097     $daysInMonth = Date_Calc::daysInMonth($month,$year);
1098    
1099     $weeksInMonth = Date_Calc::weeksInMonth($month,$year);
1100     for($row_counter=0; $row_counter < $weeksInMonth; $row_counter++)
1101     {
1102     for($column_counter=0; $column_counter <= 6; $column_counter++)
1103     {
1104     $month_array[$row_counter][$column_counter] = Date_Calc::daysToDate($curr_day,$format);
1105     $curr_day++;
1106     }
1107     }
1108    
1109     return $month_array;
1110    
1111     } // end func getCalendarMonth
1112    
1113     /**
1114     * Return a set of arrays to construct a calendar year for
1115     * the given date.
1116     *
1117     * @param string year in format CCYY, default current local year
1118     * @param string format for returned date
1119     *
1120     * @access public
1121     *
1122     * @return array $year[$month][$row][$col]
1123     */
1124    
1125     function getCalendarYear($year="",$format="%Y%m%d")
1126     {
1127     if(empty($year))
1128     $year = Date_Calc::dateNow("%Y");
1129    
1130     $year_array = array();
1131    
1132     for($curr_month=0; $curr_month <=11; $curr_month++)
1133     $year_array[$curr_month] = Date_Calc::getCalendarMonth(sprintf("%02d",$curr_month+1),$year,$format);
1134    
1135     return $year_array;
1136    
1137     } // end func getCalendarYear
1138    
1139     /**
1140     * Converts a date to number of days since a
1141     * distant unspecified epoch.
1142     *
1143     * @param string year in format CCYY
1144     * @param string month in format MM
1145     * @param string day in format DD
1146     *
1147     * @access public
1148     *
1149     * @return integer number of days
1150     */
1151    
1152     function dateToDays($day,$month,$year)
1153     {
1154    
1155     $century = substr($year,0,2);
1156     $year = substr($year,2,2);
1157    
1158     if($month > 2)
1159     $month -= 3;
1160     else
1161     {
1162     $month += 9;
1163     if($year)
1164     $year--;
1165     else
1166     {
1167     $year = 99;
1168     $century --;
1169     }
1170     }
1171    
1172     return ( floor(( 146097 * $century) / 4 ) +
1173     floor(( 1461 * $year) / 4 ) +
1174     floor(( 153 * $month + 2) / 5 ) +
1175     $day + 1721119);
1176    
1177     } // end func dateToDays
1178    
1179     /**
1180     * Converts number of days to a distant unspecified epoch.
1181     *
1182     * @param int number of days
1183     * @param string format for returned date
1184     *
1185     * @access public
1186     *
1187     * @return string date in specified format
1188     */
1189    
1190     function daysToDate($days,$format="%Y%m%d")
1191     {
1192    
1193     $days -= 1721119;
1194     $century = floor(( 4 * $days - 1) / 146097);
1195     $days = floor(4 * $days - 1 - 146097 * $century);
1196     $day = floor($days / 4);
1197    
1198     $year = floor(( 4 * $day + 3) / 1461);
1199     $day = floor(4 * $day + 3 - 1461 * $year);
1200     $day = floor(($day + 4) / 4);
1201    
1202     $month = floor(( 5 * $day - 3) / 153);
1203     $day = floor(5 * $day - 3 - 153 * $month);
1204     $day = floor(($day + 5) / 5);
1205    
1206     if($month < 10)
1207     $month +=3;
1208     else
1209     {
1210     $month -=9;
1211     if($year++ == 99)
1212     {
1213     $year = 0;
1214     $century++;
1215     }
1216     }
1217    
1218     $century = sprintf("%02d",$century);
1219     $year = sprintf("%02d",$year);
1220    
1221     return(Date_Calc::dateFormat($day,$month,$century.$year,$format));
1222    
1223     } // end func daysToDate
1224    
1225     /**
1226     * Calculates the date of the Nth weekday of the month,
1227     * such as the second Saturday of January 2000.
1228     *
1229     * @param string occurance: 1=first, 2=second, 3=third, etc.
1230     * @param string dayOfWeek: 0=Sunday, 1=Monday, etc.
1231     * @param string year in format CCYY
1232     * @param string month in format MM
1233     * @param string format for returned date
1234     *
1235     * @access public
1236     *
1237     * @return string date in given format
1238     */
1239    
1240     function NWeekdayOfMonth($occurance,$dayOfWeek,$month,$year,$format="%Y%m%d") {
1241    
1242     $year = sprintf("%04d",$year);
1243     $month = sprintf("%02d",$month);
1244    
1245     $DOW1day = sprintf("%02d",(($occurance - 1) * 7 + 1));
1246     $DOW1 = Date_Calc::dayOfWeek($month,$year,$DOW1day);
1247    
1248     $wdate = ($occurance - 1) * 7 + 1 +
1249     (7 + $dayOfWeek - $DOW1) % 7;
1250    
1251     if( $wdate > Date_Calc::daysInMonth($month,$year))
1252     return -1;
1253     else
1254     return(Date_Calc::dateFormat($month,$year,$wdate,$format));
1255    
1256     } // end func NWeekdayOfMonth
1257    
1258     /**
1259     * Formats the date in the given format, much like
1260     * strfmt(). This function is used to alleviate the
1261     * problem with 32-bit numbers for dates pre 1970
1262     * or post 2038, as strfmt() has on most systems.
1263     * Most of the formatting options are compatible.
1264     *
1265     * formatting options:
1266     *
1267     * %a abbreviated weekday name (Sun, Mon, Tue)
1268     * %A full weekday name (Sunday, Monday, Tuesday)
1269     * %b abbreviated month name (Jan, Feb, Mar)
1270     * %B full month name (January, February, March)
1271     * %d day of month (range 00 to 31)
1272     * %e day of month, single digit (range 0 to 31)
1273     * %E number of days since unspecified epoch (integer)
1274     * (%E is useful for passing a date in a URL as
1275     * an integer value. Then simply use
1276     * daysToDate() to convert back to a date.)
1277     * %j day of year (range 001 to 366)
1278     * %m month as decimal number (range 1 to 12)
1279     * %n newline character (\n)
1280     * %t tab character (\t)
1281     * %w weekday as decimal (0 = Sunday)
1282     * %U week number of current year, first sunday as first week
1283     * %y year as decimal (range 00 to 99)
1284     * %Y year as decimal including century (range 0000 to 9999)
1285     * %% literal '%'
1286     *
1287     * @param string year in format CCYY
1288     * @param string month in format MM
1289     * @param string day in format DD
1290     * @param string format for returned date
1291     *
1292     * @access public
1293     *
1294     * @return string date in given format
1295     */
1296    
1297     function dateFormat($day,$month,$year,$format)
1298     {
1299     if(!Date_Calc::isValidDate($day,$month,$year))
1300     {
1301     $year = Date_Calc::dateNow("%Y");
1302     $month = Date_Calc::dateNow("%m");
1303     $day = Date_Calc::dateNow("%d");
1304     }
1305    
1306     $output = "";
1307    
1308     for($strpos = 0; $strpos < strlen($format); $strpos++)
1309     {
1310     $char = substr($format,$strpos,1);
1311     if($char == "%")
1312     {
1313     $nextchar = substr($format,$strpos + 1,1);
1314     switch($nextchar)
1315     {
1316     case "a":
1317     $output .= Date_Calc::getWeekdayAbbrname($day,$month,$year);
1318     break;
1319     case "A":
1320     $output .= Date_Calc::getWeekdayFullname($day,$month,$year);
1321     break;
1322     case "b":
1323     $output .= Date_Calc::getMonthAbbrname($month);
1324     break;
1325     case "B":
1326     $output .= Date_Calc::getMonthFullname($month);
1327     break;
1328     case "d":
1329     $output .= sprintf("%02d",$day);
1330     break;
1331     case "e":
1332     $output .= $day;
1333     break;
1334     case "E":
1335     $output .= Date_Calc::dateToDays($day,$month,$year);
1336     break;
1337     case "j":
1338     $output .= Date_Calc::julianDate($day,$month,$year);
1339     break;
1340     case "m":
1341     $output .= sprintf("%02d",$month);
1342     break;
1343     case "n":
1344     $output .= "\n";
1345     break;
1346     case "t":
1347     $output .= "\t";
1348     break;
1349     case "w":
1350     $output .= Date_Calc::dayOfWeek($day,$month,$year);
1351     break;
1352     case "U":
1353     $output .= Date_Calc::weekOfYear($day,$month,$year);
1354     break;
1355     case "y":
1356     $output .= substr($year,2,2);
1357     break;
1358     case "Y":
1359     $output .= $year;
1360     break;
1361     case "%":
1362     $output .= "%";
1363     break;
1364     default:
1365     $output .= $char.$nextchar;
1366     }
1367     $strpos++;
1368     }
1369     else
1370     {
1371     $output .= $char;
1372     }
1373     }
1374     return $output;
1375    
1376     } // end func dateFormat
1377    
1378     /**
1379     * Returns the current local year in format CCYY
1380     *
1381     * @access public
1382     *
1383     * @return string year in format CCYY
1384     */
1385    
1386     function getYear()
1387     {
1388     return Date_Calc::dateNow("%Y");
1389    
1390     } // end func getYear
1391    
1392     /**
1393     * Returns the current local month in format MM
1394     *
1395     * @access public
1396     *
1397     * @return string month in format MM
1398     */
1399    
1400     function getMonth()
1401     {
1402     return Date_Calc::dateNow("%m");
1403    
1404     } // end func getMonth
1405    
1406     /**
1407     * Returns the current local day in format DD
1408     *
1409     * @access public
1410     *
1411     * @return string day in format DD
1412     */
1413    
1414     function getDay()
1415     {
1416     return Date_Calc::dateNow("%d");
1417    
1418     } // end func getDay
1419    
1420     /**
1421     * Returns the full month name for the given month
1422     *
1423     * @param string month in format MM
1424     *
1425     * @access public
1426     *
1427     * @return string full month name
1428     */
1429    
1430     function getMonthFullname($month)
1431     {
1432     $month = (int)$month;
1433    
1434     if(empty($month))
1435     $month = Date_Calc::dateNow("%m");
1436    
1437     $month_names = Date_Calc::getMonthNames();
1438     return $month_names[$month];
1439     // getMonthNames returns months with correct indexes
1440     //return $month_names[($month - 1)];
1441    
1442     } // end func getMonthFullname
1443    
1444     /**
1445     * Returns the abbreviated month name for the given month
1446     *
1447     * @param string month in format MM
1448     * @param int optional length of abbreviation, default is 3
1449     *
1450     * @access public
1451     *
1452     * @return string abbreviated month name
1453     * @see Date_Calc::getMonthFullname
1454     */
1455    
1456     function getMonthAbbrname($month,$length=3)
1457     {
1458     $month = (int)$month;
1459    
1460     if(empty($month))
1461     $month = Date_Calc::dateNow("%m");
1462     return substr(Date_Calc::getMonthFullname($month), 0, $length);
1463     } // end func getMonthAbbrname
1464    
1465     /**
1466     * Returns the full weekday name for the given date
1467     *
1468     * @param string year in format CCYY, default current local year
1469     * @param string month in format MM, default current local month
1470     * @param string day in format DD, default current local day
1471     *
1472     * @access public
1473     *
1474     * @return string full month name
1475     */
1476    
1477     function getWeekdayFullname($day="",$month="",$year="")
1478     {
1479     if(empty($year))
1480     $year = Date_Calc::dateNow("%Y");
1481     if(empty($month))
1482     $month = Date_Calc::dateNow("%m");
1483     if(empty($day))
1484     $day = Date_Calc::dateNow("%d");
1485    
1486     $weekday_names = Date_Calc::getWeekDays();
1487     $weekday = Date_Calc::dayOfWeek($day,$month,$year);
1488    
1489     return $weekday_names[$weekday];
1490    
1491     } // end func getWeekdayFullname
1492    
1493     /**
1494     * Returns the abbreviated weekday name for the given date
1495     *
1496     * @param string year in format CCYY, default current local year
1497     * @param string month in format MM, default current local month
1498     * @param string day in format DD, default current local day
1499     * @param int optional length of abbreviation, default is 3
1500     *
1501     * @access public
1502     *
1503     * @return string full month name
1504     * @see Date_Calc::getWeekdayFullname
1505     */
1506    
1507     function getWeekdayAbbrname($day="",$month="",$year="",$length=3)
1508     {
1509     if(empty($year))
1510     $year = Date_Calc::dateNow("%Y");
1511     if(empty($month))
1512     $month = Date_Calc::dateNow("%m");
1513     if(empty($day))
1514     $day = Date_Calc::dateNow("%d");
1515     return substr(Date_Calc::getWeekdayFullname($day,$month,$year),0,$length);
1516     } // end func getWeekdayFullname
1517    
1518     /**
1519     * Returns the numeric month from the month name or an abreviation
1520     *
1521     * Both August and Aug would return 8.
1522     * Month name is case insensitive.
1523     *
1524     * @param string month name
1525     * @return integer month number
1526     */
1527     function getMonthFromFullName($month){
1528     $month = strtolower($month);
1529     $months = Date_Calc::getMonthNames();
1530     while(list($id, $name) = each($months)){
1531     if(ereg($month, strtolower($name))){
1532     return($id);
1533     }
1534     }
1535     return(0);
1536     }
1537    
1538     /**
1539     * Retunrs an array of month names
1540     *
1541     * Used to take advantage of the setlocale function to return
1542     * language specific month names.
1543     * XXX cache values to some global array to avoid preformace hits when called more than once.
1544     *
1545     * @returns array An array of month names
1546     */
1547     function getMonthNames(){
1548     for($i=1;$i<13;$i++){
1549     $months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001));
1550     }
1551     return($months);
1552     }
1553    
1554     /**
1555     * Returns an array of week days
1556     *
1557     * Used to take advantage of the setlocale function to
1558     * return language specific week days
1559     * XXX cache values to some global array to avoid preformace hits when called more than once.
1560     *
1561     * @returns array An array of week day names
1562     */
1563     function getWeekDays(){
1564     for($i=0;$i<7;$i++){
1565     $weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001));
1566     }
1567     return($weekdays);
1568     }
1569    
1570     } // end class Date_calendar
1571    
1572     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed