PHP Get the first and last day of the week sample code_PHP tutorial

WBOY
Release: 2016-07-13 10:24:51
Original
908 people have browsed it

//The first and last day of this week

Copy code 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, I don’t know what modity is used for, so I found it Another two examples
Copy code The code is as follows:

//Monday of this week
// @$ timestamp, a timestamp of a certain week, defaults to 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 The code is as follows:

// @$timestamp, a timestamp of a certain week, the default is the current time
// @is_return_timestamp, whether Return timestamp, otherwise 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) + /*6*86400*/518400;
if($is_return_timestamp){
$cache[$id] = $sunday;
}else{
$cache[$id] = date('Y-m-d',$sunday);
}
}
return $cache[$id];
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825315.htmlTechArticle//Copy the code for the first and last day of this 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...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!