Date and common time calculation in php, and date calculation in php

Source: Internet
Author: User
Tags idate sunrise time time and seconds

Date and common time calculation in php, and date calculation in php

It was used in projects until today, yesterday, this week, this month, this quarter, this year, last week, last quarter, and so on, therefore, we plan to summarize and learn related time knowledge points of php.

1. Read the date function in the php manual.

Common time functions:

Checkdate () to verify whether a time is correct

Date_default_timezone_get () gets the time zone used by the current script

Date_default_timezone_set () can also set the time zone ini_set () used by the script, or modify the configuration file.

Date_sunrise () date_sunset () returns the sunrise time and Sunset Time of the given date and place.

Date () format a date. Details will be provided below

Getdate () obtains information about the date and time.

Gettimeofday () obtains information about the current time.

Idate () format the local time date as an integer, but only one character is accepted as the parameter

Microtime () returns the current Timestamp and number of seconds

Mktime () gets the timestamp of a date.

Strtotime () parses the time and seconds of English text into a timestamp

2. Explanation of important functions

Date () format a date

    string date( string $format [, int $timestamp] )

D indicates the day of the month, that is, the number of the day, which has the leading zero, for example ,.

D. The day of the week, which is the abbreviation of the Day of the English week. Mon to Sun

L (L lowercase) day of the week, which is in full English format, Sunday to Saturday

N represents the day of the week by number. 1 is Monday, and 7 is Sunday.

S: English suffix after the number of days per month

The day of the week. The value 0 indicates Sunday and 6 indicates Saturday.

Day 0 to 365 in year z

Week of year W

Month F, full English Words

M month number format with leading 0

M: the abbreviation of the month.

N indicates the month with no leading 0.

T the number of days in a given month

L check whether it is a leap year, a leap year is 1, and a month is 0

The year represented by four digits of Y.

The year in the y-digit format.

A lower-case morning or afternoon values

A is an upper-case morning or afternoon value.

G 12-hour system with no leading 0

G 24-hour system with no leading 0

H 12-hour system with leading 0

H 24-hour system with leading 0

I has the number of minutes leading to 0

S seconds, with leading 0

U millisecond, the date () function returns the 000000 format

E time zone ID

When I is running, it is 1, not 0

T the time zone of the Local Machine

C 15:22:21 + format time

Number of seconds since U 1970

Idate () function description

The difference with date is that this function can only pass one parameter, and date () can pass multiple parameters

B Internet time

D. day of the month

Time in the h 12-hour format

Time in the 24-hour H format

I minute

If enabled, 1 is returned. Otherwise, 0 is returned.

L if it is a leap year, 1 is returned; otherwise, 0 is returned.

M month number

S seconds

T total days of the month

The number of seconds that U starts from 1970.

The day of the week

The week of the year W, starting from Monday.

Y year, one or two digits

4-digit year Y

The day of the year z

Z timezone offset in seconds

Strtotime () function cohesion

Usage example

      strtotime ("now");      strtotime ("10 September 2017");      strtotime ("+1 day");      strtotime ("+1 week");      strtotime ("+1 week 2 days 4 hours 2 seconds");      strtotime ("next Thursday");      strtotime ("last Monday");

3. Common time Summary

$ Times = []; function makeTime () {// obtain the start and end timestamp of today. $ beginToday = mktime (0, 0, date ('M '), date ('D'), date ('y'); $ endToday = mktime (0, 0, date ('M'), date ('D') + 1, date ('y')-1; $ times ['today'] ['begin'] = $ begintointoday; $ times ['today'] ['end'] = $ endToday; // obtain the start and end timestamp of yesterday. $ beginYesterday = mktime (, 0, 0, date ('M'), date ('D')-1, date ('y'); $ endYesterday = mktime (0, 0, date ('M '), date ('D'), date ('y')-1; $ times ['yesterda Y'] ['begin'] = $ beginYesterday; $ times ['yesterday'] ['end'] = $ endYesterday; // obtain the start Timestamp and end timestamp of last week $ beginLastweek = mktime (, 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'); $ times ['lastweek'] ['begin'] = $ beginLastweek; $ times ['lastweek '] ['end'] = $ endLastweek; // obtain the start and end timestamp of the month $ beginThismonth = mktime (, 0, 0, date ('M'), 1, date ('y ')); $ EndThismonth = mktime (, 59, 59, date ('M'), date ('T'), date ('y ')); $ times ['thismonth'] ['begin'] = $ beginThismonth; $ times ['thismonth'] ['end'] = $ endThismonth; // obtain the start time and end time of the week. In this example, the start time is Monday $ defadate date = date ('Y-m-d'); $ first = 1; $ w = date ('w', strtotime ($ defaultDate); $ beginWeek = strtotime ("$ defaultDate -". ($ w? $ W-$ first: 6 ). 'days'); $ endWeek = $ beginWeek + 6*24*3600-1; $ times ['thisweek '] ['begin'] = $ beginWeek; $ times ['thisweek '] ['end'] = $ endWeek; // obtain the start and end timestamp of the previous month $ beginLastmonth = mktime (, 0, 0, date ('M')-, date ('y'); $ endLastmonth = mktime (, 59, 59, date ('M')-1, date ('T'), date ('y'); $ times ['lastmonth'] ['begin'] = $ beginLastmonth; $ times ['lastmonth'] ['end'] = $ endLastmonth; // get the start time and end time of this year $ beginThisyear = mktime (, 1, 1, date ('y'); $ endThisyear = mktime (, 59, 31, date ('y ')); $ times ['thisyear'] ['begin'] = $ beginThisyear; $ times ['thisyear'] ['end'] = $ endThisyear; // obtain the start time and end time of the previous year $ beginLastyear = mktime (, 1, date ('y')-1); $ endLastyear = mktime (, 12, 31, date ('y')-1); $ times ['lastyear'] ['begin'] = $ beginLastyear; $ times ['lastyear'] ['end'] = $ endLastyear; // obtain the start time and end Time of the current quarter $ season = ceil (date ('n ')) /3); // the quarter of the current month $ beginThisSeason = mktime (0, 0, 0, $ season * 3-3 +, date ('y ')); $ endThisSeason = mktime (, 59, 59, $ season * 3, date ('T', mktime (0, 0, 0, $ season, date ("Y"), date ('y'); $ times ['thissecason '] ['begin'] = $ beginThisSeason; $ times ['thisseason '] ['end'] = $ endThisSeason; // obtain the start time and end time of the last quarter $ beginLastSeason = mktime (0, 0, 0, 0, ($ season-1) * 3-3 +, date ('y'); $ endLastSeason = mktime (, 59, 59, ($ season-1) * 3, date ('T', mktime (0, 0, 0, $ season * 3, 1, date ("Y"), date ('y ')); $ times ['lastseason '] ['begin'] = $ ininlastseason; $ times ['lastseason'] ['end'] = $ endLastSeason; return $ times ;} $ times = makeTime ();

At present, it is the timestamp I used previously. In the future, we will accumulate summaries to avoid repeated wheel creation.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.