PHP obtains the first day of the week and the last day of the week.

Source: Internet
Author: User
PHP retrieves the first and last days of the week. // The first and last days of the week. $ date = new & nbsp; DateTime (); $ date-& gt; modify ('This & nbsp; week '); $ first_day_of_week = $ date-& gt; format ('Y-m-D'); $ date-& gt; modify PHP get the first day and last day of the week

// The first and last days of the week
$ Date = new DateTime ();
$ Date-> modify ('This Week ');
$ First_day_of_week = $ date-> format ('Y-m-D ');
$ Date-> modify ('this week + 6 days ');
$ End_day_of_week = $ date-> format ('Y-m-D ');

Using PHP to get the first day and last day of the week, there are many methods on the internet. it is too troublesome to use functions, and there are some bugs. since the official website has a DateTime class, why is it rare to see someone using it? This is the answer from a foreign website. is this method reliable?
------ Solution --------------------
Reliable, no problem
------ Solution --------------------
This week
echo date('Y-m-d w', strtotime('this week'));
2014-02-17 1

We are Chinese and do not know much about how westerners express dates (especially programmers)
Besides, this statement does not conform to the fact that the week begins with Sunday.

Calculated based on the default php date, similar to writing
echo date('Y-m-d w', strtotime('last day this week'));
2014-02-16 0

------ Solution --------------------
Reference:
This week
echo date('Y-m-d w', strtotime('this week'));
2014-02-17 1

We are Chinese and do not know much about how westerners express dates (especially programmers)
Besides, this statement does not conform to the fact that the week begins with Sunday.

Calculated based on the default php date, similar to writing
echo date('Y-m-d w', strtotime('last day this week'));
2014-02-16 0


Powerful moderator
------ Solution --------------------
With a starting point, you are not afraid of having no ending point.
echo date('Y-m-d w', strtotime('last day this week +6 day'));
2014-02-22 6

------ Solution --------------------

Echo date ('Y-m-D', mktime (0, 0, 0, date ('M'), date ('D')-date ('w '), date ('Y'); // The first day
Echo date ('Y-m-D', mktime (0, 0, 0, date ('M'), date ('D ') + (6-date ('w'), date ('Y'); // The Last Day

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.