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.