PHP Take this week, the first day of the month and the last day

Source: Internet
Author: User
Tags echo date
    1. Echo ' The first day of the week (Sunday is the beginning of the week): '. Date (' y-m-d ', Time () -86400*date (' W ')). '
    2. 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). '
    3. Echo ' first day of the month: '. Date (' y-m-d ', Mktime (0,0,0,date (' n '), 1,date (' Y ')). '
    4. Echo ' Last day of the month: '. Date (' y-m-d ', Mktime (0,0,0,date (' n '), date (' t '), date (' Y ')). '
Copy Code

2,php DATE gets the first and last day of the month! echo Date (' y-m-01 ', Time ()). ' ----'. Date (' Y-m-t ', Time ())

3,php time function The first day of the month on the first day of the given date

Code: Function Getpurmonth ($date) {//Gets the first and last day of the previous month for the specified date.

    1. $time =strtotime ($date);
    2. $firstday =date (' y-m-01 ', strtotime (date (' Y ', $time). ' -'. (Date (' m ', $time)-1). ' -01 '));
    3. $lastday =date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day");
    4. Return Array ($firstday, $lastday);
    5. }
Copy Code

The problem arises. For example get the first day, above this use (date (' m ', $time)-1); If you get a given date last month. Do you want to lose 2 directly? What if the given date is January? Will it eventually become date (' y-m-01 ', strtotime (' 2012--1-01 '))????? This is obviously a wrong time format. So this function is not suitable for extended use.

Query PHP function library, find a function mktime ();

Mktime () defines and uses the Mktime () function to return a Unix timestamp of a date. The parameter always represents the GMT date, so IS_DST has no effect on the result. Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current GMT value. Grammar Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)

Example: the Mktime () function is useful for date arithmetic and validation. It can automatically correct out-of-bounds input, what does it mean. automatically corrects out-of-bounds input. Enemies. That's what we want. Below is a look at the example

    1. echo Date (' y-m-d ', strtotime (' 2012--2-1 '));
    2. Echo '
    3. ';
    4. echo Date (' y-m-d ', Mktime (0,0,0,-2,1,2012));
Copy Code

The date 2 months before January 1, 2012 is to be output. That is October 1, 2011. 1970-01-012011-10-01 Conclusion: The first function has an error. Shows the time when the timestamp is 0. and the second function lets us get the time we want.

  • 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: 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.