- 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 (' n '), 1,date (' Y ')). '
';
- Echo ' Last day of the month: '. Date (' y-m-d ', Mktime (0,0,0,date (' n '), date (' t '), date (' Y ')). '
';
Copy Code2,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.
- $time =strtotime ($date);
- $firstday =date (' y-m-01 ', strtotime (date (' Y ', $time). ' -'. (Date (' m ', $time)-1). ' -01 '));
- $lastday =date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day");
- Return Array ($firstday, $lastday);
- }
Copy CodeThe 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
- echo Date (' y-m-d ', strtotime (' 2012--2-1 '));
- Echo '
- ';
- echo Date (' y-m-d ', Mktime (0,0,0,-2,1,2012));
Copy CodeThe 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. |