/**
- * Calculates the year, month, week, and day of two date intervals
- * Edit bbs.it-home.org
- */
- function format ($a, $b) {
- Check the two date size, the default is small after large, if the former large after the small swap position to ensure that the former small after large
- if (Strtotime ($a) >strtotime ($b)) list ($a, $b) =array ($b, $a);
- $start = Strtotime ($a);
- $stop = Strtotime ($b);
- $extend = ($stop-$start)/86400;
- $result [' extends '] = $extend;
- if ($extend <7) {//if less than 7 days direct return days
- $result [' daily '] = $extend;
- }elseif ($extend <=31) {///less than 28 days returns the number of weeks, as leap year February satisfies
- if ($stop ==strtotime ($a. ' +1 month ')) {
- $result [' monthly '] = 1;
- }else{
- $w = Floor ($extend/7);
- $d = ($stop-strtotime ($a. ' + '. $w. ' Week ')/86400;
- $result [' weekly '] = $w;
- $result [' daily '] = $d;
- }
- }else{
- $y = Floor ($extend/365);
- if ($y >=1) {//If more than one year
- $start = Strtotime ($a. ' + '. $y. ' year ');
- $a = date (' y-m-d ', $start);
- Judging whether it really has been a year, if not, then cut
- if ($start > $stop) {
- $a = date (' y-m-d ', strtotime ($a. '-1 month '));
- $m = 11;
- $y--;
- }
- $extend = ($stop-strtotime ($a))/86400;
- }
- if (Isset ($m)) {
- $w = Floor ($extend/7);
- $d = $extend-$w *7;
- }else{
- $m = Isset ($m)? $m: Round ($extend/30);
- $stop >=strtotime ($a. ' + '. $m. ' Month ')? $m: $m-;
- if ($stop >=strtotime ($a. ' + '. $m. ' Month ')} {
- $d = $w = ($stop-strtotime ($a. ' + '. $m. ' Month ')/86400;
- $w = Floor ($w/7);
- $d = $d-$w *7;
- }
- }
- $result [' yearly '] = $y;
- $result [' monthly '] = $m;
- $result [' weekly '] = $w;
- $result [' daily '] = Isset ($d)? $d: null;
- }
- Return Array_filter ($result);
- }
Print_r (Format (' 2012-10-1 ', ' 2012-12-15 '));
- ?>
Copy CodeOutput result: Array ([extends]=>75[monthly]=>2[weekly]=>2) 2,php Query the number of weeks in a day and the start date of the corresponding week
- /**
- * @file
- * @version 1.1
- */
- Gets the week number of a date, the start end time for the week
- Private Function Getweekstartendday ($day)
- {
- $g = strftime ("%u", Strtotime ($day));
- Return Array (' Week_num ' =>strftime ("%V", Strtotime ($day)), ' Week_start_day ' =>strftime ('%y-%m-%d ', Strtotime ($ Day)-($g-1) *86400), ' WEEK_START_DAY_CN ' =>strftime ('%y year%m month%d ', Strtotime ($day)-($g-1) *86400), ' week_end_day ' = >strftime ('%y-%m-%d ', Strtotime ($day) + (7-$g) *86400), ' WEEK_END_DAY_CN ' =>strftime ('%Y year%m month%d day ', Strtotime ($ Day) + (7-$g) *86400));
- }
- ?>
Copy Code |