PHP calculates the start date and end date of the week for a given date

Source: Internet
Author: User
This article mainly introduces PHP to calculate the start date and end date of the week of the given date, involving PHP date and time-related operations and conversion skills, with a certain reference value, the need for friends can refer to the next

This example describes the start and end dates of the week in which PHP calculates the given date. Share to everyone for your reference, as follows:

<?php/** * Gets the start date and end date of the week of the given date * @param string $gdate date, default to day, format: YYYY-MM-DD * @param int $weekStart Week with Monday or Sunday, 0 for week Days, 1 for Monday * @return array Array ("Start date",  "End Date"), */function getaweektimeslot ($gdate = ", $weekStart = 0) {if (! $g Date) {$gdate = date ("y-m-d"), $w = Date ("W", Strtotime ($gdate)); Get the day of the week, starting in Sunday 0-6 $dn = $w? $w-$weekStart: 6; The number of days to subtract $st = Date ("y-m-d", Strtotime ("$gdate  -". $dn. "Days  ")); $en = Date ("y-m-d", Strtotime ("$st  +6 days  "); Return Array ($st, $en); Returns the start and end dates} $timeSlot =getaweektimeslot (' 2017-01-24 ', 1); echo "Week start:{$timeSlot [0]}--->week end: {$timeSlot [ 1]} ";? >

The results of the operation are as follows:

Week start:2017-01-23--->week end:2017-01-29

The above is the whole content of this article, I hope that everyone's study has helped.


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.