php 获取日期(当天,前天,明天,本周,本月,本季度,本年)

<?php
echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";

$begin_day = date("Y-m-d 00:00:00");
$end_day = date("Y-m-d 23:59:59");

$last_begin = date("Y-m-d 00:00:00",strtotime("last days"));
$last_end = date("Y-m-d 23:59:59",strtotime("last days"));

$next_begin = date("Y-m-d 00:00:00",strtotime("next days"));
$next_end = date("Y-m-d 23:59:59",strtotime("next days"));

$month_times = strtotime(date("Y-m"));
$monthday = date("t");
$month = date("n");
$begin_this_week=date("Y-m-d 00:00:00",strtotime("last Sunday +1 day"));
$end_this_week=date("Y-m-d 23:59:59",strtotime("next Sunday"));

$begin_this_month=date('Y-m-01 00:00:00');
$end_this_month=date("Y-m-$monthday 23:59:59",strtotime("this month",$month_times));

if($month==1 || $month==2 ||$month==3){
 $begin_this_quarter=date('Y-01-01 00:00:00');
 $end_this_quarter=date("Y-03-31 23:59:59");
}elseif($month==4 || $month==5 ||$month==6){
 $begin_this_quarter=date('Y-04-01 00:00:00');
 $end_this_quarter=date("Y-06-30 23:59:59");  
}elseif($month==7 || $month==8 ||$month==9){
 $begin_this_quarter=date('Y-07-01 00:00:00');
 $end_this_quarter=date("Y-09-30 23:59:59");  
}else{
 $begin_this_quarter=date('Y-10-01 00:00:00');
 $end_this_quarter=date("Y-12-31 23:59:59");    
}

$begin_this_year=date('Y-01-01 00:00:00');
$end_this_year=date('Y-12-31 23:59:59');

//一季度是1月1日到3月31日;二季度是4月1日到6月30;三季度是7月1到9月30;四季度是10月1到12月31
echo "{$begin_day}当天{$end_day} <br/>";
echo "{$last_begin}前天{$last_end} <br/>";
echo "{$next_begin}明天{$next_end} <br/>";
echo "{$begin_this_week}本周{$end_this_week} <br/>";
echo "{$begin_this_month}本月{$end_this_month} <br/>";
echo "{$begin_this_quarter}本季度{$end_this_quarter} <br/>";
echo "{$begin_this_year}本年{$end_this_year} <br/>";


=================================php date 详解=========================================================

d  月份中的第几天,有前导零的 2 位数字 01 到 31 

D  星期中的第几天,文本表示,3 个字母 Mon 到 Sun 

j  月份中的第几天,没有前导零 1 到 31 

l  (“L”的小写字母) 星期几,完整的文本格式 Sunday 到 Saturday 

N  ISO-8601 格式数字表示的星期中的第几天(PHP 5.1.0 新加) 1(星期一)到 7(星期天) 

S  每月天数后面的英文后缀,2 个字符 st,nd,rd 或者 th。可以和 j 一起用 

w  星期中的第几天,数字表示 0(星期天)到 6(星期六) 

z  年份中的第几天 0 到 366 

W  ISO-8601 格式年份中的第几周,每周从星期一开始(PHP 4.1.0 新加的) 42(当年的第 42 周) 

F  月份,完整的文本格式,例如 January 或者 March January 到 December 

m  数字表示的月份,有前导零 01 到 12 

M  三个字母缩写表示的月份 Jan 到 Dec 

n  数字表示的月份,没有前导零 1 到 12 

t  给定月份所应有的天数 28 到 31 

L  是否为闰年 如果是闰年为 1,否则为 0 

o  ISO-8601 格式年份数字。

Y  4 位数字完整表示的年份 例如:1999 或 2003 

y  2 位数字表示的年份 例如:99 或 03 

a  小写的上午和下午值 am 或 pm 

A  大写的上午和下午值 AM 或 PM 

B  Swatch Internet 标准时 000 到 999 

g  小时,12 小时格式,没有前导零 1 到 12 

G  小时,24 小时格式,没有前导零 0 到 23 

h  小时,12 小时格式,有前导零 01 到 12 

H  小时,24 小时格式,有前导零 00 到 23 

i  有前导零的分钟数 00 到 59> 

s  秒数,有前导零 00 到 59> 

e  时区标识(PHP 5.1.0 新加) 例如:UTC,GMT,Atlantic/Azores 

I  是否为夏令时 如果是夏令时为 1,否则为 0 

O  与格林威治时间相差的小时数 例如:+0200 

P  与格林威治时间(GMT)的差别,小时和分钟之间有冒号分隔 例如:+02:00 

T  本机所在的时区  

Z  时差偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边是正的。 -43200 到 43200 

c  ISO 8601 格式的日期(PHP 5 新加) 2004-02-12T15:19:21+00:00 

r  RFC 822 格式的日期 例如:Thu, 21 Dec 2000 16:01:07 +0200 

U  从 Unix 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数 time()获得时间戳 

php的date()函数十分强大,通过上面这些参数可以实现很多日期的操作,比如说获取上面我标记的红色参数说明来实现今天是星期几操作!


  

原文地址:https://www.cnblogs.com/abinlove/p/3750047.html