Php obtains the start and end dates of the week and last week.

Source: Internet
Author: User

Recently, due to work needs, php is used to obtain the start date and end date of the current week and last week. I found it online, but it was not suitable. So I made a summary myself. The specific content and Code are as follows.

<? Phpheader ('content-type: text/html; charset = UTF-8 '); $ date = date ('Y-m-d'); // current date $ first = 1; // $ first = 1 indicates that Monday is the start date 0, indicating that every Sunday is the start date $ w = date ('w', strtotime ($ date )); // obtain the day of the current week. The day from Sunday is 0 Monday to Saturday is 1-6 $ now_start = date ('Y-m-d', strtotime ("$ date -". ($ w? $ W-$ first: 6 ). 'Days '); // obtain the start date of the week. If $ w is 0, it indicates Sunday, minus 6 days $ now_end = date ('Y-m-d ', strtotime ("$ now_start + 6 days"); // end date of the week $ last_start = date ('Y-m-d ', strtotime ("$ now_start-7 days"); // last week's start date $ last_end = date ('Y-m-d ', strtotime ("$ now_start-1 days"); // last week end date echo 'start date of the week: ', $ now_start,' <br/> '; echo 'end date of the week: ', $ now_end,' <br/> '; echo 'start date of the last week:', $ last_start, '<br/> '; echo 'end Date of last week: ', $ last_end,' <br/> ';

Note: Since the weeks in foreign countries start from Sunday and China is used to start from Monday, an error occurs when strtotime ("last Monday") is used directly.

Articles you may be interested in
  • Php obtains the start and end timestamps of today, yesterday, last week, and this month.
  • Php obtains the start time and end time timestamp of the week of the specified date.
  • PHP retrieves the date list for the first 30 days of the current date
  • Php extracts the birthday date from the ID card number and verifies whether it is a minor.
  • Reasons why PHP adds a backslash before the quotation mark and how PHP removes the backslash, there are three ways to disable the php magic quotation mark
  • Php calculates the number of years, months, and days between two dates.
  • Thinkphp automatic verification and automatic filling are invalid Solutions
  • PHP obtains the week of the specified date, the first day of the month and the last day of the month.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.