PHP obtains sample code for the first and last days of the week.

Source: Internet
Author: User

PHP obtains sample code for the first and last days of the week.

This article describes how to obtain the first and last days of the week using PHP. For more information, see

// The first and last days of the week

The Code is as follows:

$ 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 ');

 

After testing modity, I did not know what to do. I found another two examples.

The Code is as follows:

// Monday of the week

// @ $ Timestamp, a timestamp of a week. The default value is the current time.

// @ Is_return_timestamp, whether to return the timestamp; otherwise, the return time format

Function this_monday ($ timestamp = 0, $ is_return_timestamp = true ){

Static $ cache;

$ Id = $ timestamp. $ is_return_timestamp;

If (! Isset ($ cache [$ id]) {

If (! $ Timestamp) $ timestamp = time ();

$ Monday_date = date ('Y-m-d', $ timestamp-86400 * date ('w', $ timestamp) + (date ('w', $ timestamp)> 0? 86400:-/* 6*86400 */518400 ));

If ($ is_return_timestamp ){

$ Cache [$ id] = strtotime ($ monday_date );

} Else {

$ Cache [$ id] = $ monday_date;

}

}

Return $ cache [$ id];

}

 

// Sunday of the week

The Code is as follows:

// @ $ Timestamp, a timestamp of a week. The default value is the current time.

// @ Is_return_timestamp, whether to return the timestamp; otherwise, the return time format

Function this_sunday ($ timestamp = 0, $ is_return_timestamp = true ){

Static $ cache;

$ Id = $ timestamp. $ is_return_timestamp;

If (! Isset ($ cache [$ id]) {

If (! $ Timestamp) $ timestamp = time ();

$ Sunday = this_monday ($ timestamp) +/* 6x86400 */518400;

If ($ is_return_timestamp ){

$ Cache [$ id] = $ Sunday;

} Else {

$ Cache [$ id] = date ('Y-m-d', $ Sunday );

}

}

Return $ cache [$ id];

}

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.