PHP start time and end time of the week; start time end of month; end of month start time

Source: Internet
Author: User
Tags echo date

<?php/** * Function: Gets the start date and end date of the week of the given date * parameters: $gdate date, default is the day, format: YYYY-MM-DD * $first Week with Monday or Sunday start, 0 for Sunday, 1 for Monday * return: Array arr Ay ("Start date", "End Date"); * */function aweek ($gdate = "", $first = 0) {if (! $gdate) $gdate = Date ("y-m-d"); $w = Date ("W", Strtotime ($gdate));//Get the day of the week, Sunday start 0-6 $dn = $w? $w-$first: 6;//The number of days to subtract//the start date of the Week $st = Date ("y-m-d", Strtotime ("$gdate-". $dn.)); This week's end date $en = Date ("y-m-d", Strtotime ("$st +6 Days"); Last week start date $last _st = Date (' y-m-d ', Strtotime ("$st-7 Days")); Last week end Date $last _en = Date (' y-m-d ', Strtotime ("$st-1 Days")); Return Array ($st, $en, $last _st, $last _en);//returns the start and end dates}echo implode ("|", Aweek ("", 1)). ' <br/> ';//echo date ("y-m-d", Strtotime ("Time ()"); Echo ' The first day of the week (Sunday is the beginning of the week): '. Date (' y-m-d ', Time () -86400*date (' W ') )).‘ <br/> ' echo ' The first day of the week (Monday is the beginning of the week): '. Date (' y-m-d ', Time () -86400*date (' W ') + (date (' W ') >0?86400:-6*86400). ' <br/> '; Echo ' first day of the month: '. Date (' y-m-d ', Mktime (0,0,0,date (' m '), 1,date (' Y ')). ' <br/> '; Echo ' last Day of the month: '. Date (' y-m-d ', Mktime (0,0, 0,date (' m '), date (' t '), date (' Y ')). <br/> ';//Last month's start date $m = Date (' y-m-d ', Mktime (0,0,0,date (' m ') -1,1,date (' Y ')));//How many days last month $t = date (' t ', Strtotime (" $m ")); Echo ' first day of the month: '. Date (' y-m-d ', Mktime (0,0,0,date (' m ') -1,1,date (' Y ')). ' <br/> '; Echo ' last Last day: '. Date (' y-m-d ', Mktime (0,0,0,date (' m ')-1, $t, date (' Y ')). ' <br/> ';? The >php manual has a method that returns the Monday and Sunday <?phpfunction get_week_range ($week, $year) {$timestamp = Mktime (1,0,0,1,1, $year) for the specified date    ;    $firstday = Date ("N", $timestamp); if ($firstday >4) $firstweek = strtotime (' + '). (    8-$firstday). ' Days ', $timestamp); else $firstweek = strtotime ('-'). (        $firstday-1). ' Days ', $timestamp); $monday = strtotime (' + '). (    $week-1). ' Week ', $firstweek);        $sunday = Strtotime (' +6 days ', $monday);    $start = Date ("y-m-d", $monday);        $end = Date ("y-m-d", $sunday); Return Array ($start, $end);}? >strtotime get the first day of the week and the last day method of the Bugphp manual has a method that returns the Monday and Sunday of the specified date <?phpfunction get_week_range ($week, $year) {$ timestamp = Mktime (1,0,0,1,1, $year);    $firstday = Date ("N", $timestamp); if ($firstday >4) $firstweek = strtotime (' + '). (    8-$firstday). ' Days ', $timestamp); else $firstweek = strtotime ('-'). (        $firstday-1). ' Days ', $timestamp); $monday = strtotime (' + '). (    $week-1). ' Week ', $firstweek);        $sunday = Strtotime (' +6 days ', $monday);    $start = Date ("y-m-d", $monday);        $end = Date ("y-m-d", $sunday); Return Array ($start, $end);}? > But in the cross-year time use will have problems such as 2009 of December 31 Thursday and January 1, 2010 Friday weeks get Monday and Sunday completely different December 31, 2009 to the Monday and Sunday respectively corresponding 2009-12-282010-01-03 but January 1, 2010 take To Monday and Sunday corresponds to 2011-01-032011-01-09 original because the method of the week for the 53rd week, but the year is 2010, so think 2010 of the 53rd week, so the calculation is wrong, the solution is, if the week is greater than 10 (because January months can not have 10 weeks), And the month is 1, the year minus 1 is processed if (date (' m ', $last _week_time) = = ' _last_week ' and $tmp >) {$last _week_year--;}

  

PHP start time and end time of the week; start time end of month; end of month start time

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.