PHP Gets the _php instance of a year with several weeks and a weekly start date and end date

Source: Internet
Author: User
Tags learn php

Recently, one of the requirements was to use PHP to get a few weeks of a year and a weekly start date and contact date. Find some information on the Internet is not appropriate, so I made a copy, the following two ways to achieve PHP acquisition of a year has several weeks and the weekly start date and end date

Code One:

<?php
Header ("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,, $year));
Echo $year. ' A total of years '. $weeks. ' Zhou <br/> ';
if ($week > $weeks | | $week <= 0)
{
 $week = 1;
}
if ($week <)
{
 $week = ' 0 '. $week;
}
$timestamp [' start '] = Strtotime ($year. ' W '. $week);
$timestamp [' end '] = Strtotime (' +1 week-1 Day ', $timestamp [' Start ']);
Echo $year. ' Year of the first '. $week. ' Week start timestamp: '. $timestamp [' Start ']. ' <br/> ';
Echo $year. ' Year of the first '. $week. ' Week end timestamp: '. $timestamp [' End ']. ' <br/> ';
Echo $year. ' Year of the first '. $week. ' Week start date: '. Date ("y-m-d", $timestamp [' Start ']). ' <br/> ';
Echo $year. ' Year of the first '. $week. ' Week End Date: '. Date ("y-m-d", $timestamp [' End ']);
? >

Code two:

<?php
Header ("Content-type:text/html;charset=utf-8");
function Getisoweeksinyear ($year)
{
 $date = new DateTime;
 $date->setisodate ($year);
 Return ($date->format ("W") = = "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 ' <br> Weeks in 2013<br> '. Getisoweeksinyear (2013);
$weekday = Weekday (Strtotime (' D-m-y ', strtotime (' 5-8-2013 ')));
Echo ' <br> 10-8-2013 ';
Echo ' <br>start: '. $weekday [0];
Echo ' <br>end: '. $weekday [1];
? >

The entire contents of this article, I hope that you learn PHP to get a year has several weeks and the weekly start date and end date, helpful.

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.