-
-
/** - * Calculate the number of years, months, weeks and days between two dates
- * edit bbs.it-home.org
- */
- function format($a,$b){
- //Check the size of two dates, the default is small in the front and large in the back, if If the front is big and the back is small, the positions are swapped to ensure that the front is small and the back is big
- 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 it is less than 7 days, return the number of days directly
- $result['daily'] = $extend;
- }elseif($extend<=31){ //If it is less than 28 days, return the number of weeks, because February of the leap year meets
- 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);
- //Determine whether it has really been a year, if not, reduce it
- 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);
- }< ;/p>
print_r(format('2012-10-1','2012-12-15'));
- ?>
-
Copy code
output result:
Array([extends]=>75[monthly]=>2[weekly]=>2)
2. PHP queries the week number of a certain day and the starting date of the corresponding week
-
- /**
- * @file
- * @version 1.1
- */
- //Get the week number of a certain date and the corresponding start and end time of 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 day',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
|