This article will introduce to you some examples of using PHP to specify date data (this week, last week, this month, last month, this quarter) using strtotime or mktime. I hope it will be helpful to you.
strtotime definition and usage
Thestrtotime() function parses any English text datetime description into a Unix timestamp.
Grammar
strtotime(time,now) parameter description
time specifies the time string to be parsed.
now is the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.
Example
The code is as follows | Copy code | ||||
";
echo date("Y-m-d",strtotime("-1 week Monday")), " echo date("w");//What day of the week echo date("t");//number of days in this month
echo " |
mktime function
The mktime() function returns the Unix timestamp of a date.
The argument always represents a GMT date, so is_dst has no effect on the result.
代码如下 | 复制代码 | |
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')))," "; $jdtoday = gregoriantojd(date('n'), date('j'), date('y')); $offset = jddayofweek($jdtoday)-1; for($i=0-$offset; $i<7-$offset; $i++){ $date = strtotime($i.' days'); echo(' | '.date('D', $date).'/'.date('n-j', $date).' | ');
---|
The code is as follows | Copy code | |
echo date("Y-m-d H:i:s ",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")))," "; echo " This week: "; 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 " Last month: "; 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 " This month: "; 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")));// Number of days in the last month of the quarter echo " This quarter: "; 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')) )," "; $jdtoday = gregoriantojd(date('n'), date('j'), date('y')); $offset = jddayofweek($jdtoday)-1; for($i=0-$offset; $i<7-$offset; $i++){<🎜> $date = strtotime($i.' days');<🎜> echo(' | '.date('D', $date).'/'.date('n-j', $date).' | '); } ?>
---|
Parameters | Description |
---|
参数 | 描述 |
---|---|
hour | 可选。规定小时。 |
minute | 可选。规定分钟。 |
second | 可选。规定秒。 |
month | 可选。规定用数字表示的月。 |
day | 可选。规定天。 |
year | 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。 |
is_dst |
可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。 自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。 |