[PHP Learning Tutorial-date/Time]001. First day of month & last day

Source: Internet
Author: User

Introduction: In the time processing, for the front/background humanized display of the current month maximum number of days , this is the site must deal with an aspect. But usually this piece is packaged directly by a third-party class library, where we make a simple mark.

Today, we provide you with a function to get the first and last day of a given month !

Please read the tutorial directly!

function function

The function is very simple, and the following is the way to put it

Syntax:array (firstday=>, lastday=>) = Firstandlastday(year, month)

Parameter resolution:

$y-year

$m-month

return Result:

  Array (month start time timestamp, month end time timestamp)

<?PHP/** * Gets the timestamp of the beginning of the first day of the specified month and the end of the last day * * @param int $y year $m month * @return Array (month start time, end of month)*/functionFristandlastday ($y= "",$m= "") {    if($y== "")$y=Date("Y"); if($m== "")$m=Date("M"); $m=sprintf("%02d",intval($m)); $y=Str_pad(intval($y), 4, "0",str_pad_right); $m>12 | |$m<1?$m=1:$m=$m; $firstday=Strtotime($y.$m. "01000000"); $firstdaystr=Date("y-m-01",$firstday); $lastday=Strtotime(Date(' Y-m-d 23:59:59 ',Strtotime("$firstdaystr+1 month-1 Day "))); return Array(        "FirstDay" =$firstday, "lastday" =$lastday    );}?>

Note: This example does not format the returned result day, whether add 0 or no 0, the developer can re-update the function to meet their own requirements. :)

Example 1:

<?PHP//Query Year$year= ' 2015 ';//Query Month$month= ' 10 ';//get the first day and the last day$data= Fristandlastday ($year,$month);//Output ResultsEcho("First day Timestamp: {$data[' FirstDay ']} <br/> ");Echo("Last day Timestamp: {$data[' Lastday ']} <br/> ");

Conclusion

Just do a simple mark to sign PHP to learn the new category-< date , and open it up.

Not finished, waiting to be updated ...

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4822607.html

[PHP Learning Tutorial-date/Time]001. First day of month & last day

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.