ES0en gets the sample code for the first and last days of the week

  • 2021-01-11 01:55:19
  • OfStack

// The first and last day 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'); 

After testing, modity does not know what it is used for. I found another two examples at that time
 
// The week of the week 1 
// @$timestamp  , some day of the week 1 The default is the current time  
// @is_return_timestamp , Whether to return a timestamp, or a 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]; 
} 

// It's Sunday this week
 
// @$timestamp  , some day of the week 1 The default is the current time  
// @is_return_timestamp , Whether to return a timestamp, or a 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]; 
} 

Related articles: