Recently, due to work needs, I use php to get the start date and end date of the current week and last week. I searched online and found nothing suitable, so I made a summary myself. The specific content and code are as follows.
<?php
header('Content-type: text/html; charset=utf-8');
$date=date('Y-m-d'); //当前日期
$first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$w=date('w',strtotime($date)); //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$now_end=date('Y-m-d',strtotime("$now_start +6 days")); //本周结束日期
$last_start=date('Y-m-d',strtotime("$now_start - 7 days")); //上周开始日期
$last_end=date('Y-m-d',strtotime("$now_start - 1 days")); //上周结束日期
echo '本周开始日期:',$now_start,'<br />';
echo '本周结束日期:',$now_end,'<br />';
echo '上周开始日期:',$last_start,'<br />';
echo '上周结束日期:',$last_end,'<br />';
Copy after login
Note: Since the week in foreign countries always starts on Sunday, and in China, it is customary to start on Monday, so directly using strtotime("last monday") will give wrong results.
Articles you may be interested in
- php How to get the start timestamp and end timestamp of today, yesterday, last week, and this month
- php Get the timestamp of the start time and end time of the week on the specified date
- PHP Get the date list 30 days before the current date
- PHP extract the birthday date in the ID number and verify whether it is unknown Functions for adults
- The reason why PHP adds a backslash before the quotation mark and how to remove the backslash in PHP, three ways to close php magic quotation marks
- php calculates the number of years between two dates , how many months and how many days function
- thinkphp automatic verification and automatic filling invalid solution
- PHP gets the first and last day of the week and month of the specified date
http://www.bkjia.com/PHPjc/779409.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779409.htmlTechArticleRecently, due to work needs, I use php to get the start date and end date of the current week and last week. I searched online and found nothing suitable, so I made a summary myself. Specific content and code...