Home > Backend Development > PHP Tutorial > php get the start date and end date of this week and last week_PHP tutorial

php get the start date and end date of this week and last week_PHP tutorial

WBOY
Release: 2016-07-13 10:29:00
Original
1016 people have browsed it

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

www.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...
Related labels:
php
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