PHP obtains the number of weeks in a year and the start and end dates of each week.

Source: Internet
Author: User
This article mainly introduces how many weeks php calculates in a year, and obtains the start and end dates of each week. If you need them, refer to them.

This article mainly introduces how many weeks php calculates in a year, and obtains the start and end dates of each week. If you need them, refer to them.

Recently, I took over a project. One of the requirements was to use php to obtain the weeks of the year, the start date of the week, and the contact date. I did not find the appropriate information on the Internet, so I made a copy of it myself. Below I will use two methods to obtain the number of weeks of the year in PHP, as well as the start date and end date of the week.

Code 1:

<? Phpheader ("Content-type: text/html; charset = UTF-8"); date_default_timezone_set ("Asia/Shanghai"); $ year = (int) $ _ GET ['Year']; $ week = (int) $ _ GET ['Week ']; $ weeks = date ("W", mktime (0, 0, 0, 12, 28, $ year); echo $ year. 'years in total '. $ weeks. 'Week
'; If ($ week> $ weeks | $ week <= 0) {$ week = 1;} if ($ week <10) {$ week = '0 '. $ week;} $ timestamp ['start'] = strtotime ($ year. 'W '. $ week); $ timestamp ['end'] = strtotime ('+ 1 week-1 Day', $ timestamp ['start']); echo $ year. 'Year '. $ week. 'Week start timestamp :'. $ timestamp ['start'].'
'; Echo $ year. 'Year'. $ week. 'Week end timestamp:'. $ timestamp ['end'].'
'; Echo $ year. 'Year '. $ week. 'Week start date :'. date ("Y-m-d", $ timestamp ['start']).'
'; Echo $ year. 'Year '. $ week. 'Week end date :'. date ("Y-m-d", $ timestamp ['end']);?>

Code 2:

<? Phpheader ("Content-type: text/html; charset = UTF-8"); function getIsoWeeksInYear ($ year) {$ date = new DateTime; $ date-> setISODate ($ year, 53); return ($ date-> format ("W") = "53 "? 53: 52);} function weekday ($ custom_date) {$ week_start = date ('d-m-y', strtotime ('This week Monday', $ custom_date )); $ week_end = date ('d-m-Y ', strtotime ('This week Sunday', $ custom_date); $ week_array [0] = $ week_start; $ week_array [1] = $ week_end; return $ week_array;} echo'
Weeks in 2013
'. GetIsoWeeksInYear (2013); $ weekday = weekday (strtotime (date ('d-m-y', strtotime ('5-8-2013'); echo'
10-8-2013 '; echo'
Start: '. $ weekday [0]; echo'
End: '. $ weekday [1];?>

All of the above content will be helpful for you to learn how many weeks of the year, weekly start date, and end date for PHP.

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.