PHP uses strtotime or mktime to specify date data (this week, last week, this month, last month, this quarter)_PHP tutorial

WBOY
Release: 2016-07-13 10:50:02
Original
930 people have browsed it

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

The

strtotime() 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("now")), "
";
echo date("Y-m-d",strtotime("-1 week Monday")), "
"; //离现在最近的周一
echo date("Y-m-d",strtotime("-1 week Sunday")), "
"; //离现在最近的周末
echo date("Y-m-d",strtotime("+0 week Monday")), "
"; //将要到来的周一
echo date("Y-m-d",strtotime("+0 week Sunday")), "
"; //将要到来的周末
echo date("n");// 第几个月
echo date("w");//本周周几
echo date("t");// 本月天数
echo "
上周:
";

echo date("Y-m-d",strtotime("now")), "
";

echo date("Y-m-d",strtotime("-1 week Monday")), "
"; //The closest Monday to now
echo date("Y-m-d",strtotime("-1 week Sunday")), "
"; //The closest weekend to now echo date("Y-m-d",strtotime("+0 week Monday")), "
"; //The coming Monday
echo date("Y-m-d",strtotime("+0 week Sunday")), "
"; //The upcoming weekend

echo date("n");//month

echo date("w");//What day of the week

echo date("t");//number of days in this month

echo "
Last week:
";

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 parameters can be left empty in order from right to left, and the empty parameters will be set to the corresponding current GMT value. Grammar mktime(hour,minute,second,month,day,year,is_dst) Example '); } ?>
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 参数被废弃。因此应该使用新的时区处理特性。

hour Optional. Specified hours. minute Optional. Specified minutes.

second
Optional. Specifies seconds. month Optional. Specifies the numeric month. day Optional. Specify days. year Optional. Specified year. On some systems, legal values ​​are between 1901 - 2038. However, this limitation no longer exists in PHP 5. is_dst Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 if unknown. As of 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used.
http://www.bkjia.com/PHPjc/632679.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632679.htmlTechArticleThis article will introduce to students how to use php to specify date data (this week, last week, This month, last month, this quarter) examples, I hope it will be helpful to all classmates. str...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template