当前位置 : 主页 > 网络编程 > PHP >

PHP 日期格式化(获取上月第一天、最后一天等)

来源:互联网 收集:自由互联 发布时间:2021-06-11
1.获取上个月第一天及最后一天. echo date('Y-m-01',strtotime('-1 month')); echo "br/"; echo date('Y-m-t',strtotime('-1 month')); echo "br/";2.获取当月第一天及最后一天. $BeginDate=date('Y-m-01',strtotime(date("Y-m-d"))
1.获取上个月第一天及最后一天. echo date('Y-m-01',strtotime('-1 month')); echo "<br/>"; echo date('Y-m-t',strtotime('-1 month')); echo "<br/>"; 2.获取当月第一天及最后一天. $BeginDate=date('Y-m-01',strtotime(date("Y-m-d"))); echo $BeginDate; echo "<br/>"; echo date('Y-m-d',strtotime("$BeginDate +1 month -1 day")); echo "<br/>"; 3.获取当天年份、月份、日及天数. echo " 本月共有:".date("t")."天"; echo " 当前年份".date('Y'); echo " 当前月份".date('m'); echo " 当前几号".date('d'); echo "<br/>"; 4.使用函数及数组来获取当月第一天及最后一天,比较实用 function getthemonth($date) { $firstday = date('Y-m-01',strtotime($date)); $lastday = date('Y-m-d',strtotime("$firstday +1 month -1 day")); return array($firstday,$lastday); } $today = date("Y-m-d"); $day=getthemonth($today); echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1]; echo "<br/>";

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


网友评论