PHP Time and date operations

Source: Internet
Author: User
Tags echo date

1Date_default_timezone_set (' PRC ');//Default time zone2 //Current time increased by 5 days3 $date 1= "2014-11-11";4 Echo Date(' y-m-d ',Strtotime("$date 1+5 Day "));//output Result: 2014-11-165 ///Accordingly, to increase the month, year, change day to Month6  7  8 //+++ today, Yesterday, tomorrow, last week, next week +++++++++9 Echo"Today:",Date("Y-m-d", Time()), "; Ten Echo"Yesterday:",Date("Y-m-d",Strtotime("-1 Day"), ";  One Echo"Tomorrow:",Date("Y-m-d",Strtotime("+1 Day"), ";  A Echo"A week later:",Date("Y-m-d",Strtotime("+1 Week"), ";  - Echo"2 days a week, four hours, two seconds later:",Date("Y-m-d g:h:s",Strtotime("+1 Week 2 days 4 hours 2 Seconds"), ";  - Echo"Next week Four:",Date("Y-m-d",Strtotime("Next Thursday"), ";  the Echo"Last Monday:".Date("Y-m-d",Strtotime("Last Monday")). " ;  - Echo"One months ago:".Date("Y-m-d",Strtotime("Last month"). " ;  - Echo"One months later:".Date("Y-m-d",Strtotime("+1 Month")). " ;  - Echo"Ten years later:".Date("Y-m-d",Strtotime("+10 Year")). " 

1**2*function: Gets the start date and end date of the week of the given date3Parameters$gdateDate, default is day, format: yyyy-mm-DD4*$firstThe week begins in Monday or Sunday, 0 is Sunday, 1 is Monday5* Return: Array of arrays ("Start date", "End Date");6*7*/8  9 functionAweek ($gdate= "",$first= 0){Ten     if(!$gdate)$gdate=Date("y-m-d"); One     $w=Date("W",Strtotime($gdate));//get the day of the week, starting in Sunday 0-6 A     $dn=$w?$w-$first: 6;//number of days to subtract - //Start date of the week -     $st=Date("Y-m-d",Strtotime("$gdate-".$dn. "Days")); the     //end of week 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);//return to start and end dates A } at   - 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 ')). ' ; - 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). ' ; - Echo' First day of the month: '.Date(' y-m-d ',Mktime(0,0,0,Date(' m '), 1,Date(' Y ')). ' ; in Echo' Last day of the month: '.Date(' y-m-d ',Mktime(0,0,0,Date(' m '),Date(' t '),Date(' Y ')). ' ; - //last month's start date to $m=Date(' y-m-d ',Mktime(0,0,0,Date(' m ') -1,1,Date(' Y '))); + //How many days were there last month - $t=Date(' t ',Strtotime("$m")); the Echo' First day of the month: '.Date(' y-m-d ',Mktime(0,0,0,Date(' m ') -1,1,Date(' Y ')). ' ; * Echo' Last day of last month: '.Date(' y-m-d ',Mktime(0,0,0,Date(' m ')-1,$t,Date(' Y ')). ' 

====================================

1 //PHP Manual has this method to return the specified date of Monday and Sunday2 functionGet_week_range ($week,$year){3     $timestamp=Mktime(1,0,0,1,1,$year);4     $firstday=Date("N",$timestamp);5     if($firstday>4){6         $firstweek=Strtotime(' + '. (8-$firstday). ' Days ',$timestamp);7}Else{8         $firstweek=Strtotime(‘-‘. ($firstday-1). ' Days ',$timestamp);9     }Ten     $monday=Strtotime(' + '. ($week-1). ' Week ',$firstweek); One     $sunday=Strtotime(' +6 days ',$monday); A   -     $start=Date("Y-m-d",$monday); -     $end=Date("Y-m-d",$sunday); the   -     return Array($start,$end); - } -   + //Strtotime Get the first day of the week and the last day method bug -   + the//php manual has this method for returning the Monday and Sunday of a specified date A   at functionGet_week_range2 ($week,$year){ -     $timestamp=Mktime(1,0,0,1,1,$year); -     $firstday=Date("N",$timestamp); -     if($firstday>4){ -         $firstweek=Strtotime(' + '. (8-$firstday). ' Days ',$timestamp); -}Else{ in         $firstweek=Strtotime(‘-‘. ($firstday-1). ' Days ',$timestamp); -     } to     $monday=Strtotime(' + '. ($week-1). ' Week ',$firstweek); +     $sunday=Strtotime(' +6 days ',$monday); -   the     $start=Date("Y-m-d",$monday); *     $end=Date("Y-m-d",$sunday); $  Panax Notoginseng     return Array($start,$end); -}

But there's a problem with using it across the years.
For example 2011 of December 31 Saturday and January 1, 2012 Sunday, get the Monday and Sunday completely different
Monday and Sunday respectively corresponding to the December 31, 2011
2011-12-26
2012-01-01

But the January 1, 2012 and Sunday were the same as Monday
2012-01-02
2012-01-04

The reason for this is the week of the 53rd week of the method passed in, but the year is 2011, so think 2011 of the 53rd week, so the calculation is wrong, the solution is,
If the week is greater than 10 (because January months are unlikely to have 10 weeks) and the month is 1, the year minus 1 is processed

1 if (date(' m ',$last _week_time$tmp _last_week >) {2     $last _ Week_year--; 3 }

PHP Time and 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.