PHP gets the start and end dates of each week of the year _php tutorial

Source: Internet
Author: User

How PHP gets the start and end dates of each week of the year


This article mainly introduces PHP to get the start and end date of each week of the year, involving PHP on the date operation of the skills, with a certain reference value, the need for friends can refer to the following

This example describes how PHP gets the start and end dates of each week of the year. Share to everyone for your reference. The specific analysis is as follows:

In recent projects, you need to make a weekly submission feature that requires you to know the start and end dates for a specified number of weeks to handle other business. Here is a piece of code to get the start date and end date of the week in a year through PHP, and share it with you.
The following is a code for getting the start and end dates of a week in a year through PHP.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

function Get_week ($year) {

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

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

$startday = Strtotime ($year _start);

if (intval (' 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);//Gets the date of the first week of the year

$endday = Strtotime ($year _end);

if (intval (' 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 number of weeks of the first and last day of the year by passing in the parameter $year year, calculates the date of the first week, and gets the date of the first and last day of each week by looping. The last return is an array.
To get the start and end dates for a specified number of weeks, such as the start and end dates for the 18th Week of 2011, the code is as follows:

?

1

2

3

$weeks = Get_week (2011);

Echo ' 18th week start date: '. $weeks [18][0].

Echo ' 18th week End Date: '. $weeks [18][1];

The result of the final output:

?

1

2

1.18th Week Start Date: 2011.05.02

2.18th Week End Date: 2011.05.08

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/957539.html www.bkjia.com true http://www.bkjia.com/PHPjc/957539.html techarticle how PHP gets the start and end dates of each week of the year this article focuses on PHP's approach to getting the start and end dates for each week of the year, involving PHP on date operations ...

  • 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.