PHP get the first day of the week and the last day sample code _php tips

Source: Internet
Author: User
The first and last day of the week
Copy Code code as follows:

$date =new DateTime ();
$date->modify (' this week ');
$first _day_of_week= $date->format (' y-m-d ');
$date->modify (' This week for +6 days ');
$end _day_of_week= $date->format (' y-m-d ');

After testing modity do not know what to do, in time to find another two examples
Copy Code code as follows:

Monday of this week
@ $timestamp, a time stamp of a week, the default is the current time
@is_return_timestamp, whether to return a timestamp, otherwise return the 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 this week
Copy Code code as follows:

//@ $timestamp, a time stamp for a week, defaults to the current time
//@is_return_timestamp, returns a timestamp, Otherwise returns the 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) +/*6*86400*/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.