PHP各种获取日期的方法,获取日期对应星期、一周日期、星期开始与结束日期的汇总方法

各种获取日期方法


var_dump(date('Y-m-d H:i:s'));
var_dump(date('w'));
var_dump(date('Y-m-d',strtotime('-1 day',time())));
var_dump(date('Y-m-d',strtotime('-1 week',time())));
var_dump(date('Y-m-d',strtotime('-1 month',time())));
var_dump(date('Y-m-d',strtotime('-1 year',time())));


echo "当天:".date("Y-m-d",strtotime("now")), "\n";
echo "本周星期一日期:".date("Y-m-d",strtotime("-1 week Monday")). "\n";
echo "上周星期天日期:".date("Y-m-d",strtotime("-1 week Sunday")). "\n";
echo "下周周星期一日期:".date("Y-m-d",strtotime("+0 week Monday")). "\n";
echo "本周星期天日期:".date("Y-m-d",strtotime("+0 week Sunday")). "\n";
// 上周一
$time = date("Y-m-d",strtotime("-1 week last monday"));
echo "上周一日期:".($time). "\n";;
// 本周一
$time = date("Y-m-d",strtotime("0 week last monday"));
echo "本周一日期:".($time). "\n";;
// 下周一
$time = date("Y-m-d",strtotime("1 week last monday"));
echo "周一日期:".($time). "\n";;

echo "
"
; echo "*********第几个月:" ."\n"; echo date('n') ."\n"; echo "*********本周周几:0(星期天) 1(星期一)" ."\n"; echo date("w") ."\n"; echo "*********本月天数:" ."\n"; echo date("t") ."\n"; echo "*********" ."\n"; echo '
上周起始时间:
'
; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n"; echo '
本周起始时间:
'
; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n"; //从第几周找出该周的开始日期和结束日期 $dayNumber = date('W') * 7; $weekDayNumber = date("w", mktime(0, 0, 0, 1, $dayNumber, date("Y")));//当前周的第几天 $startNumber = $dayNumber - $weekDayNumber; echo date("Y-m-d", mktime(0, 0, 0, 1, $startNumber + 1, date("Y")));//开始日期 echo date("Y-m-d", mktime(0, 0, 0, 1, $startNumber + 7, date("Y")));//结束日期 echo '
上月起始时间:
'
; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n"; echo '
本月起始时间:
'
; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n"; $season = ceil((date('n'))/3);//当月是第几季度 echo '
本季度起始时间:
'
; echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n"; echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n"; $season = ceil((date('n'))/3)-1;//上季度是第几季度 echo '
上季度起始时间:
'
; echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n"; echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";

汇总方法

/*
 * 获取日期对应的星期 
 * @param string $date 日期数据,格式如:2018-6-22
 */
function  get_week($date){	
	$date_str=date('Y-m-d',strtotime($date));//强制转换日期格式	
	$arr=explode("-", $date_str);//封装成数组
	//参数赋值	
	$year=$arr[0];//年	
	$month=sprintf('%02d',$arr[1]);    //月,输出2位整型,不够2位右对齐	
	$day=sprintf('%02d',$arr[2]);//日,输出2位整型,不够2位右对齐	
	$hour = $minute = $second = 0;//时分秒默认赋值为0;	
	$strap = mktime($hour,$minute,$second,$month,$day,$year);//转换成时间戳	
	$number_wk=date("w",$strap);//获取数字型星期几
	//自定义星期数组
	$weekArr=array("周日","周一","周二","周三","周四","周五","周六");
	//获取数字对应的星期
	return $weekArr[$number_wk];
}

 /**
   * 获取一周日期
   * @param int $time 时间戳
   * @param string $format 转换格式
   */
  function get_week_arr($time=0, $format = "Y-m-d"){
      if(!$time){
          $time = time();
      }
      $week = date('w', $time);
      $weekname = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');
      //星期日排到末位
      if (empty($week)) {
          $week = 7;
      }
      for ($i = 0; $i <= 6; $i++) {
          $currDay = $i+1-$week;
          $data[$i]['date'] = date($format, strtotime("+{$currDay} days", $time));
          $data[$i]['week'] = $weekname[$i];
      }
      return $data;
  }
   /*
	 * 获取某星期的开始时间和结束时间
	 * @param string $time 时间
	 * @param int $first 表示每周星期一为开始日期 0表示每周日为开始日期
   */
  function getWeekBeginEnd($time = '', $first = 1){
      //当前日期
      if(!$time){
          $time = time();
      }
      $sdefaultDate = date("Y-m-d", $time);
      //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
      $w = date('w', strtotime($sdefaultDate)); //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
      $days = $w ? $w - $first : 6;   //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
      $week_start = date('Y-m-d', strtotime("{$sdefaultDate} -{$days} days"));
      $week_end = date('Y-m-d', strtotime("{$week_start} +6 days"));   //本周结束日期
      return array("week_start" => $week_start, "week_end" => $week_end);
  }

你可能感兴趣的:(PHP,php)