PHP Time function date and common time calculation text

Source: Internet
Author: User
Tags idate php mysql sunrise and sunset times
This article mainly introduces the time function date in PHP and the relevant knowledge of time calculation, which has a good reference value. Let's take a look at the little series.

have been in the project need to use to today, yesterday, this week, this month, this quarter, this year, last month, the previous quarter and so on time stamp, while the recent time is more sufficient, so plan to PHP time knowledge points to summarize learning

1. Read the PHP manual date function

Common time Functions:

Checkdate () Verify that a time is correct

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

Date_default_timezone_set () Setting the time zone Ini_set () of the script can also be met, or modify the configuration file

Date_sunrise () Date_sunset () returns the sunrise and sunset times for a given date and place

Date () formats a day with details below

GETDATE () Get information about date and time

Gettimeofday () Get information about the current time

Idate () Formats the local time date as an integer, but accepts only one character as a parameter

Microtime () returns the current timestamp and number of seconds

Mktime () Gets the timestamp of a date

strtotime () resolves the date seconds of the English text to a timestamp

2, the important function of the detailed

Date () format one day

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

The day of the D-month, which is the number, which has a leading zero, for example 01,02

The day of the week in D, which is the abbreviation for the English week, Mon to Sun

L (l lowercase) Day of the week, this is the full English format, Sunday to Saturday

N figures for day of the week, 1 for Monday, 7 for Sunday

S English suffix after the number of days per month

The day of the W week, using numbers, 0 for Sunday, 6 for Saturday

Days 0 to 365 in the Z-year

The week of the W year

F month, full English word

M-month number format with leading 0

M Three-letter abbreviated month

N number represents the month, without leading 0

t the number of days that a given month should have

L detection is a leap year, leap year is 1, month is 0

Y 4-bit number represents the year

Y 2-bit number represents the year

The value of a lowercase morning or afternoon

The value of a capital morning or afternoon

G 12-hour system without leading 0

G 24-hour system without leading 0

H 12-hour system with leading 0

H 24-hour system with leading 0

I have the number of minutes leading 0

S number of seconds with leading 0

U milliseconds, the date () function returns the 000000 format of the

E Time Zone identification

I is daylight saving time, is 1, not 0

T the time zone where the machine is located

C 2017-05-08t time in 15:22:21+00:00 format

U-S number of seconds since 1970

Idate () function explanation

The difference from date is that the function can pass only one parameter, and date () can pass multiple arguments

B Internet Time

The day of the D-month

H 12-hour time

H 24-hour time

I minute

I If daylight saving time is enabled returns 1, otherwise 0

L returns 1 if it is a leap year, otherwise 0

Number of the M-month

S number of seconds

T total number of days this month

U number of seconds from 1970

The day of the W Week

The first few weeks of the W year, starting from Monday

Y year, 1 or 2 digits

Y Year 4 digits

The day ordinal of the Z-year

Z time zone offset in seconds

Strtotime () function join

Usage examples

Strtotime ("Now"), Strtotime ("September"), Strtotime ("+1 Day"), Strtotime ("+1 Week"); Strto Time ("+1 Week 2 days 4 hours 2 Seconds"), Strtotime ("next Thursday"); Strtotime ("Last Monday");

3, Common time Summary

$times = [];function maketime () {//Get today start timestamp and end timestamp $beginToday =mktime (0,0,0,date (' m '), date (' d '), date (' Y '));  $endToday =mktime (0,0,0,date (' m '), date (' d ') +1,date (' Y '))-1;  $times [' Today '] [' begin '] = $beginToday;  $times [' Today '] [' end '] = $endToday;  Gets 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;  $times [' Yesterday '] [' begin '] = $beginYesterday;  $times [' Yesterday '] [' end '] = $endYesterday;  Get last week 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 '));  $times [' Lastweek '] [' begin '] = $beginLastweek;  $times [' Lastweek '] [' end '] = $endLastweek;  Gets the start timestamp and end timestamp of the month $beginThismonth =mktime (0,0,0,date (' m '), 1,date (' Y '));  $endThismonth =mktime (23,59,59,date (' m '), date (' t '), date (' Y '));  $times [' Thismonth '] [' begin '] = $beginThismonth;  $times [' Thismonth '] [' end '] = $endThismonth; Gets the start time and end time of the week, in this exampleMonday $defaultDate = 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;  Gets the starting timestamp and end timestamp of the last month $beginLastmonth =mktime (0,0,0,date (' m ') -1,1,date (' Y '));  $endLastmonth =mktime (23,59,59,date (' m ') -1,date (' t '), date (' Y '));  $times [' Lastmonth '] [' begin '] = $beginLastmonth;  $times [' Lastmonth '] [' end '] = $endLastmonth;  Gets the start time and end time of this year $beginThisyear = Mktime (0,0,0,1,1,date (' Y '));  $endThisyear = Mktime (23,59,59,12,31,date (' Y '));  $times [' thisyear '] [' begin '] = $beginThisyear;  $times [' thisyear '] [' end '] = $endThisyear;  Gets the start time and end time of the previous year $beginLastyear = Mktime (0,0,0,1,1,date (' Y ')-1);  $endLastyear = Mktime (23,59,59,12,31,date (' Y ')-1);  $times [' lastyear '] [' begin '] = $beginLastyear;  $times [' lastyear '] [' end '] = $endLastyear; Gets the start and end time of the quarter $season = Ceil ((date (' n '))/3);//The month is the first quarter $beginThisSEason = mktime (0, 0, 0, $season *3-3+1,1,date (' Y '));  $endThisSeason = Mktime (23,59,59, $season *3,date (' t ', mktime (0, 0, 0, $season *3,1,date ("y")), date (' Y '));  $times [' Thisseason '] [' begin '] = $beginThisSeason;  $times [' Thisseason '] [' end '] = $endThisSeason;  Gets the start time and end time of the last quarter $beginLastSeason = mktime (0, 0, 0, ($season-1) *3-3+1,1,date (' Y '));  $endLastSeason = Mktime (23,59,59, ($season-1) *3,date (' t ', mktime (0, 0, 0, $season *3,1,date ("y"))), date (' Y '));  $times [' Lastseason '] [' begin '] = $beginLastSeason;  $times [' Lastseason '] [' end '] = $endLastSeason; return $times;} $times = Maketime ();

At present, I used the time stamp, the latter will accumulate a summary, avoid repeating the wheel.

Related recommendations:

Introduction to PHP date function and how to use it

Content about the PHP MySQL update statement

PHP Verification Code class Validatecode

Related Article

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.