Php mktime function obtains the start and end time of today.

Source: Internet
Author: User

The following describes how to obtain the start time and end time of the current day. The code is as follows:

Mktime (hour, minute, second, month, day, year, is_dst)

Parameter description
Hour is optional. The specified hour.
Minute is optional. Minutes.
Second is optional. Specified seconds.
Month is optional. Indicates the month in number.
Day is optional. Specified day.
Year is optional. Specified year. In some systems, the valid value ranges from 1901 to 2038. However, this restriction does not exist in PHP 5.
Is_dst
Optional. If the time is in the daylight saving time (DST) period, it is set to 1; otherwise, it is set to 0. If it is unknown, it is set to-1.

Example start timestamp and end timestamp of today, yesterday, last week, and this month

The code is as follows: Copy code

// Start time cut

$ Start = mktime (0, 0, 0, date ("m"), date ("d"), date ("Y "));

// End time:
$ End = mktime (0, 0, 0, date ('M'), date ('D') + 1, date ('Y')-1;

// Php obtains the start timestamp and end timestamp of yesterday.
 
$ BeginYesterday = mktime (0, 0, 0, date ('M'), date ('D')-1, date ('Y '));
 
$ EndYesterday = mktime (0, 0, 0, date ('M'), date ('D'), date ('Y')-1;
 
// Php obtains the last week's start timestamp and end timestamp.
 
$ BeginLastweek = mktime (0, 0, 0, date ('M'), date ('D')-date ('w') + 1-7, date ('Y '));
 
$ EndLastweek = mktime (23, 59, 59, date ('M'), date ('D')-date ('w') + 7-7, date ('Y '));
 
// Php obtains the start timestamp and end timestamp of the current month.
 
$ BeginThismonth = mktime (0, 0, 0, date ('M'), 1, date ('Y '));
 
$ EndThismonth = mktime (, 59, 59, date ('M'), date ('t'), date ('Y '));

Convert functions into Chinese

The code is as follows: Copy code

Function get_period_time ($ type = 'day '){
$ Rs = FALSE;
$ Now = time ();
Switch ($ type ){
Case 'day': // today
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', $ now );
$ Rs ['endtime'] = date ('Y-m-d 23:59:59 ', $ now );
Break;
Case 'week': // this week
$ Time = '1' = date ('w ')? Strtotime ('Monday', $ now): strtotime ('last Monday', $ now );
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', $ time );
$ Rs ['endtime'] = date ('Y-m-d 23:59:59 ', strtotime ('Sunday', $ now ));
Break;
Case 'month': // this month
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', mktime (0, 0, 0, date ('M', $ now ), '1', date ('Y', $ now )));
$ Rs ['endtime'] = date ('Y-m-d 23:39:59 ', mktime (0, 0, 0, date ('M', $ now ), date ('t', $ now), date ('Y', $ now )));
Break;
Case '3month': // three months
$ Time = strtotime ('-2 month', $ now );
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', mktime (0, date ('M', $ time), 1, date ('Y', $ time )));
$ Rs ['endtime'] = date ('Y-m-d 23:39:59 ', mktime (0, 0, 0, date ('M', $ now ), date ('t', $ now), date ('Y', $ now )));
Break;
Case 'half _ year': // within six months
$ Time = strtotime ('-5 month', $ now );
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', mktime (0, date ('M', $ time), 1, date ('Y', $ time )));
$ Rs ['endtime'] = date ('Y-m-d 23:39:59 ', mktime (0, 0, 0, date ('M', $ now ), date ('t', $ now), date ('Y', $ now )));
Break;
Case 'year': // within this year
$ Rs ['begintime'] = date ('Y-m-d 00:00:00 ', mktime (0, 1, 1, date ('Y ', $ now )));
$ Rs ['endtime'] = date ('Y-m-d 23:39:59 ', mktime (0, 0, 0, 12, 31, date ('Y ', $ now )));
Break;
    }
Return $ rs;
}

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.