PHP calculates the weekly 1st week of the year

Source: Internet
Author: User
 recently received a task, summed up, is: To calculate the annual, weekly week of 1st period.
   Thought for a while, looked at the next date function, in-depth understanding of the date function of the meaning of each parameter, finally the problem to make. In the date () function, there is a parameter that is key to solving the problem, the format character D. It represents the day ordinal of the week, expressed in text, 3 letters: from Mon to Sun.
   We can calculate the day of the week by the current time, then compare it to Monday, minus the corresponding number of days. The following sections are code: 
ublic function Caluatetime () {
        $now = date ("YMD");//Current time
        $day  = Date ("D");
        Switch ($day) {case
            ' Mon ': return
                $now;
                break;
            Case "Tue": Return
                Date ("Ymd", Strtotime (" -1days", Strtotime ($now));
                break;
            Case "Wed": Return
                Date ("Ymd", Strtotime (" -2days", Strtotime ($now));
                break;
            Case "Thu": Return
                Date ("Ymd", Strtotime (" -3days", Strtotime ($now));
                break;
            Case "Fri": Return
                Date ("Ymd", Strtotime (" -4days", Strtotime ($now));
                break;
            Case "Sat": Return
                Date ("Ymd", Strtotime (" -5days", Strtotime ($now));
                break;
            Case "Sun": Return
                Date ("Ymd", Strtotime (" -6days", Strtotime ($now));
                break;
        }
    }

To extend it, you can calculate the date of any day of the year in each week.

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.