PHP date函数参数详解
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧
不过这样方便计算,
要找出前一天的时间就是 time()-60*60*24;
要找出前一年的时间就是 time()*60*60*24*365
那么如何把这个数字换成日期格式呢,就要用到date函数了
$t=time();
echo date("Y-m-d H:i:s",$t);
第一个参数的格式分别表示:
a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"
D - 星期几,三个英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小时制的小时; 如: "01" 至 "12"
H - 24 小时制的小时; 如: "00" 至 "23"
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"
i - 分钟; 如: "00" 至 "59"
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"
M - 月份,三个英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序数,二个英文字母; 如: "th","nd"
t - 指定月份的天数; 如: "28" 至 "31"
U - 总秒数
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位数字; 如: "1999"
y - 年,二位数字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"
其它不在上列的字符则直接列出该字符
看完上面的内容再看下面的代码有助于理解
网上比较流行的代码段一
echo date("Ymd",strtotime("now")), "
";
echo date("Ymd",strtotime("-1 week Monday")), "
";
echo date("Ymd",strtotime("-1 week Sunday")), "
";
echo date("Ymd",strtotime("+0 week Monday")), "
";
echo date("Ymd",strtotime("+0 week Sunday")), "
";
echo "*********第几个月:";
echo date("n");
echo "*********本周周几:";
echo date("w");
echo "*********本月天数:";
echo date("t");
echo "*********";
echo "
上周起始时间:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"
";
echo "
本周起始时间:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"
";
//从第几周找出该周的开始日期和结束日期
$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"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"
";
echo "
本月起始时间:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"
";
$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"))),"
";
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"))),"
";
$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"))),"
";
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"))),"
";
?>
效果图:20180331
代码段二
echo date("Ymd",strtotime("now")), "
";
echo date("Ymd",strtotime("-1 week Monday")), "
";
echo date("Ymd",strtotime("-1 week Sunday")), "
";
echo date("Ymd",strtotime("+0 week Monday")), "
";
echo date("Ymd",strtotime("+0 week Sunday")), "
";
//date("n") 第几个月
//date("w") 本周周几
//date("t") 本月天数
echo "
上周:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"
";
echo "
本周:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"
";
echo "
上月:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"
";
echo "
本月:
";
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"
";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"
";
$getMonthDays = date("t",mktime(0, 0 , 0,date("n")+(date("n")-1)%3,1,date("Y")));//本季度未最后一月天数
echo "
本季度:
";
echo date("Y-m-d H:i:s", mktime(0, 0, 0,date("n")-(date("n")-1)%3,1,date("Y"))),"
";
echo date("Y-m-d H:i:s", mktime(23,59,59,date("n")+(date("n")-1)%3,$getMonthDays,date("Y"))),"
";
这段代码就介绍到这了,希望大家以后多多支持网页设计。