Home > Backend Development > PHP Tutorial > PHP gets the first and last day of this week and this month

PHP gets the first and last day of this week and this month

WBOY
Release: 2016-07-25 08:57:00
Original
1806 people have browsed it
  1. echo 'The first day of the week (the week starts on Sunday):'.date('Y-m-d', time()-86400*date('w')).'
    ';
  2. echo 'The first day of this week (the week starts on Monday):'.date('Y-m-d', time()-86400*date('w')+(date('w')> 0?86400:-6*86400)).'
    ';
  3. echo 'The first day of this month:'.date('Y-m-d', mktime(0,0,0,date(' n'),1,date('Y'))).'
    ';
  4. echo 'The last day of this month:'.date('Y-m-d', mktime(0,0,0, date('n'),date('t'),date('Y'))).'
    ';
Copy code

2, PHP DATE gets the first day of the month And the last day! echo date('Y-m-01',time()).'----'.date('Y-m-t',time())

3, PHP time function: the first day of the next month on a given date, the first day of the current month

Code: function GetPurMonth($date){//Get 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, to get the first day, use (date('m',$time)-1); what if you want to get the previous month of a given date. Should we directly subtract 2? 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. Therefore, this function is not suitable for extended use.

Query the PHP function library and find a function mktime();

mktime() Definition and usage The mktime() function returns the Unix timestamp of a date. The argument always represents a GMT date, so is_dst has no effect on the result. The parameters can be left empty in order from right to left, and the 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 operations and validation. It can automatically correct out-of-bounds input; What does it mean. Automatically correct out-of-bounds input. Opponent. This is what we want. Let’s look at an example below

  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 the code

to output the dates 2 months before January 1, 2012. That is October 1, 2011. 1970-01-01 2011-10-01 in conclusion: The first function has an error. The time with timestamp 0 is shown. And the second function gives us the time we want.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template