php時間處理

2021-06-27 00:07:16 字數 2860 閱讀 7431

php時間處理 使用strtotime mktime 配合 date 

原理: 計算時間戳  date格式化

看例子:

//先看簡單的

echo '';

date_default_timezone_set('prc');

echo "今天:",date("y-m-d",time()),"

";echo "今天:",date("y-m-d",strtotime("now")),"

";echo "今天:",date("y-m-d",strtotime("13 nov 2014")),"

";//將描述性日期格式化

echo "昨天:(-1 day)",date("y-m-d",strtotime("-1 day")),"

";echo "明天:(+1 day)",date("y-m-d",strtotime("+1 day")),"

";echo "第幾個月:",date("n"),"

";echo "本週週幾:",date("n"),"

";echo "本月天數:",date("t"),"

";echo "一周後:(+1 week)",date("y-m-d",strtotime("+1 week")),"

";echo "上乙個周一:(-1 week monday)",date("y-m-d",strtotime("-1 week monday")),"

";echo "上乙個周一:(last monday)".date("y-m-d",strtotime("last monday")),"

";echo "上乙個週末:(-1 week sunday)",date("y-m-d",strtotime("-1 week sunday")),"

";echo "下乙個周一:(+0 week monday)",date("y-m-d",strtotime("+0 week monday")),"

";echo "下乙個周一:(0week monday)",date("y-m-d",strtotime("0week monday")),"

";echo "本周四:(+0 week thursday)",date("y-m-d",strtotime("+0 week thursday")),"

";echo "下乙個周四:(next thursday)",date("y-m-d",strtotime("next thursday")), "

";echo "本週末:(0 week sunday)",date("y-m-d",strtotime("0 week sunday")),"

";echo "一周零兩天三小時四秒後:(+1 week 2 days 3 hours 4 seconds)",date("y-m-d g:h:s",strtotime("+1 week 2 days 3 hours 4 seconds")),"

";echo "一月前:(last month)".date("y-m-d",strtotime("last month")),"

";echo "一月後:(+1 month)".date("y-m-d",strtotime("+1 month")),"

";echo "十年後:(+10 year)".date("y-m-d",strtotime("+10 year")),"

";//下面的比較複雜了

echo '

上週:';

echo date("y-m-d h:i:s",mktime(0, 0 , 0,date("m"),date("j")-date("n")-7+1,date("y"))),"

";echo date("y-m-d h:i:s",mktime(23,59,59,date("m"),date("j")-date("n")+7-7,date("y"))),"

";echo '

本週:';

echo date("y-m-d h:i:s",mktime(0, 0 , 0,date("m"),date("j")-date("n")+1,date("y"))),"

";echo date("y-m-d h:i:s",mktime(23,59,59,date("m"),date("j")-date("n")+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'))),"

";die;

這裡貼上乙個日期驗證函式 

var_dump(checkdate(2, 29, 2001));

php 時間處理

php view plain copy print?echo 今天 date y m d echo 昨天 date y m d strtotime 1 day echo 明天 date y m d strtotime 1 day echo 一周後 date y m d strtotime 1 wee...

PHP 時間處理

date default timezone set asia shanghai 設定時區 一 格式化時間函式date 返回string params string date string param1,intparam1,inttime 1.date函式 對比printf sprintf 函式 da...

PHP時間處理

time time 獲取當前時間,時間戳 var dump time int 1576034056 時間戳轉換為日期格式 time1 date y m d h i s time var dump time1 string 19 2019 12 11 03 13 53 日期格式轉換為時間戳 time2...