Home >Backend Development >PHP Tutorial >How to get the year, month, day timestamp and date in php
PHP obtains the timestamp through the PHP time() function, and PHP obtains the date through the PHP data() function. How are they implemented? Specific examples are given below.
date_default_timezone_set("Asia/Shanghai"); date_default_timezone_set('PRC');//这两种方法效果相同
Time stamp conversion can be done by using date('Y-m-s h:i:s', specific timestamp)
Date conversion Timestamp, use strtotime("date()")
.
//获取今日开始时间戳和结束时间戳 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; //获取昨日起始时间戳和结束时间戳 $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y')); $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1; //获取本周起始时间戳和结束时间戳 $beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')); $endThisweek=time(); //获取上周起始时间戳和结束时间戳 $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')); //获取本月起始时间戳和结束时间戳 $beginThismonth=mktime(0,0,0,date('m'),1,date('Y')); $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y')); //上个月的起始时间: $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month'))); $end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day'))); $begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始 $end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束 //现在的时间到第二天凌晨相差的时间戳 $time = (strtotime(date('Y-m-d'))+3600*24) - time() ;
echo '<br>上周起始时间:<br>'; 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 '<br>本周起始时间:<br>'; 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"; echo '<br>上月起始时间:<br>'; 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 '<br>本月起始时间:<br>'; 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"; //本年起始 echo date(‘Y-01-01’); //结束日期 echo date(‘Y-12-31’);
Related recommendations:
Share the method of outputting time by date() function in PHP
The above is the detailed content of How to get the year, month, day timestamp and date in php. For more information, please follow other related articles on the PHP Chinese website!