Home > Backend Development > PHP Tutorial > PHP method to get the start and end dates of each week of the year_PHP tutorial

PHP method to get the start and end dates of each week of the year_PHP tutorial

WBOY
Release: 2016-07-13 10:06:46
Original
775 people have browsed it

How to get the start and end dates of each week of the year in PHP

This article mainly introduces how to get the start and end dates of each week of the year in PHP The method involves PHP's date operation skills and has certain reference value. Friends in need can refer to it

The example in this article describes how to get the start and end dates of each week of the year in PHP. Share it with everyone for your reference. The specific analysis is as follows:

In a recent project, I need to create a function to submit weekly reports. I need to know the start and end dates of specified weeks in order to handle other business. The following is a piece of code that uses PHP to get the start date and end date of each week of the year to share with you.
The following is a code that uses PHP to get the start date and end date of each week of the year.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

function get_week($year) {

$year_start = $year . "-01-01";

$year_end = $year . "-12-31";

$startday = strtotime($year_start);

if (intval(date('N', $startday)) != '1') {

$startday=strtotime("nextmonday",strtotime($year_start));

//获取年第一周的日期

}

$year_mondy = date("Y-m-d", $startday);//获取年第一周的日期

$endday = strtotime($year_end);

if (intval(date('W', $endday)) == '7') {

$endday=strtotime("lastsunday",strtotime($year_end));

}

$num = intval(date('W', $endday));

for ($i = 1; $i <= $num; $i++) {

$j = $i -1;

$start_date = date("Y-m-d", strtotime("$year_mondy $j week "));

$end_day = date("Y-m-d", strtotime("$start_date +6 day"));

$week_array[$i] = array(

str_replace("-",".",$start_date),str_replace("-",".",$end_day));

}

return $week_array;

}

1 2

3

4

5

6

1

2

3

$weeks = get_week(2011);

echo '第18周开始日期:'.$weeks[18][0].'';

echo '第18周结束日期:'.$weeks[18][1];

7 8

9

10

11

12

1

2

1. 第18周开始日期:2011.05.02

2. 第18周结束日期:2011.05.08

13 14 15 16 17 18 19 20 21 22 23
function get_week($year) { $year_start = $year . "-01-01"; $year_end = $year . "-12-31"; $startday = strtotime($year_start); if (intval(date('N', $startday)) != '1') { $startday=strtotime("nextmonday",strtotime($year_start)); //Get the date of the first week of the year } $year_mondy = date("Y-m-d", $startday);//Get the date of the first week of the year $endday = strtotime($year_end); if (intval(date('W', $endday)) == '7') { $endday=strtotime("lastsunday",strtotime($year_end)); } $num = intval(date('W', $endday)); for ($i = 1; $i <= $num; $i++) { $j = $i -1; $start_date = date("Y-m-d", strtotime("$year_mondy $j week ")); $end_day = date("Y-m-d", strtotime("$start_date +6 day")); $week_array[$i] = array( str_replace("-",".",$start_date),str_replace("-",".",$end_day)); } return $week_array; }
The function get_week() gets the week number of the first and last day of the year by passing in the parameter $year, calculates the date of the first week, and obtains the date of the first and last day of each week through a loop. The final return is an array. If you want to get the start date and end date of a specified week, such as the start date and end date of week 18 in 2011, the code is as follows: ?
1 2 3 $weeks = get_week(2011); echo 'Start date of week 18:'.$weeks[18][0].''; echo 'End date of week 18:'.$weeks[18][1];
Final output result: ?
1 2 1. Week 18 start date: 2011.05.02 2. End date of Week 18: 2011.05.08

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/957539.htmlTechArticleHow to get the start and end dates of each week in the year with PHP. This article mainly introduces how to get the start and end dates of each week in the year with PHP. The method of starting and ending dates of each week in the year involves PHP's date operations...
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