PHP Method for getting dates corresponding to week week date week start and end dates
- 2021-10-24 19:09:03
- OfStack
This article illustrates the method of PHP to get the date corresponding to week, week date, week start and end date. Share it for your reference, as follows:
/*
* Gets the week corresponding to the date
* Parameter $date Is the date data entered in the format as: 2018-6-22
*/
function get_week($date)
{
// Cast date format
$date_str = date('Y-m-d', strtotime($date));
// Encapsulate into an array
$arr = explode("-", $date_str);
// Parameter assignment
// Year
$year = $arr[0];
// Month, output 2 Bit integer, not enough 2 Bit right alignment
$month = sprintf('%02d', $arr[1]);
// Day, output 2 Bit integer, not enough 2 Bit right alignment
$day = sprintf('%02d', $arr[2]);
// The default assignment of time division and second is 0 ;
$hour = $minute = $second = 0;
// Convert to timestamp
$strap = mktime($hour, $minute, $second, $month, $day, $year);
// Get the numeric day of the week
$number_wk = date("w", $strap);
// Custom week array
$weekArr = array("0", "1", "2", "3", "4", "5", "6");
// Gets the week corresponding to the number
return $weekArr[$number_wk];
}
/**
* Get 1 Day of the week
* @param $time Time stamp
* @param $format Conversion format
*/
function get_week($time, $format = "Y-m-d") {
$week = date('w',$time);
$weekname=array(' Week 1',' Week 2',' Week 3',' Week 4',' Week 5',' Week 6',' Sunday ');
// Ranked last on Sunday
if(empty($week)){
$week=7;
}
for ($i=0;$i<=6;$i++){
$data[$i]['date'] = date($format,strtotime( '+'. $i+1-$week .' days',$time));
$data[$i]['week'] = $weekname[$i];
}
return $data;
}
/*
* Get the start time and end time of a week
* time Time
* first Means weekly week 1 Is the start date 0 Indicates that every Sunday is the start date
*/
function getWeekMyActionAndEnd($time = '', $first = 1)
{
// Current date
if (!$time) $time = time();
$sdefaultDate = date("Y-m-d", $time);
//$first =1 Means weekly week 1 Is the start date 0 Indicates that every Sunday is the start date
// Gets the day ordinal of the current week Sunday is 0 Week 1 To the week 6 Yes 1 - 6
$w = date('w', strtotime($sdefaultDate));
// Gets the start date of the week, if $w Yes 0 Is Sunday, minus 6 Days
$week_start = date('Y-m-d', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));
// End of week date
$week_end = date('Y-m-d', strtotime("$week_start +6 days"));
return array("week_start" => $week_start, "week_end" => $week_end);
}
PS: Here are some time and date related tools for your reference:
Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi
Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc
Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq
Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime
More readers interested in PHP can check the topic of this site: "php Date and Time Usage Summary", "PHP Array (Array) Operation Skills Encyclopedia", "PHP Basic Grammar Introduction Course", "PHP Operation and Operator Usage Summary", "php Object-Oriented Programming Introduction Course", "PHP Network Programming Skills Summary", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Course" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.