PHP date Functions and PHP time functions

Source: Internet
Author: User
Tags idate time zones locale setting sunrise time
This article mainly introduces the PHP date function and PHP time function, has a certain reference value, now share to everyone, the need for friends can refer to

One:
The checkdate () function validates a date.
Checkdate (Month,day,year)
If the specified value is valid, the function returns True, otherwise false is returned.
The date is valid in the following cases:
Month is between and includes 1-12
The value of day is within the range of days that a given month should have, and leap years are taken into account.
Year is between and includes 1 to 32767
Example 1:

<?phpvar_dump (Checkdate (12,31,2000)); The//var_dump function shows the type and value of the expression var_dump (Checkdate (2,29,2003)); Var_dump ( Checkdate (2,29,2004));? > Display: BOOL (TRUE) bool (FALSE) bool (TRUE) example 2:<?phpif (Checkdate (12,31,2009)) {echo "a";} Else{echo "B";}? >

Show:

A

The date_default_timezone_set () function sets the default time zone for all date/time functions used in the script.
Date_default_timezone_set (TimeZone)
TimeZone required. The time zone identifier, such as "UTC" or "Europe/paris".
List of legitimate time zones: http://www.php.net/manual/en/timezones.php
Example

<?phpecho (Date_default_timezone_set ("Europe/paris"));? >

Show:

1

Three
The date_default_timezone_get () function returns the default time zone used by all date-time functions in the script.
Date_default_timezone_get (void)
void optional

<?phpecho (Date_default_timezone_get ());? >

Show:

Asia/shanghai

Four:
The time () function returns the Unix timestamp for the current time.
Returns the number of seconds since the Unix era (January 1, 1970 00:00:00 GMT) to the current time.
The timestamp of the time the request was initiated was saved in $_server[' Request_time ' from PHP 5.1.
Example:

<?php$t=time (); Echo ($t. "<br/>"); Echo (Date ("D F D Y", $t)); >
1138618081//This is the total number of seconds Mon January 30 2006

five:
date () function formats a local time/date. The
Gmdate () function formats the GMT/UTC date/time. Similar to the date () function, the difference is that the time returned is Greenwich Mean Time (GMT).
Date (format,timestamp)
format must specify how results are returned
A-"AM" or "PM"
A-"AM" or "PM"
D-days, two digits, if less than two bits are preceded by 0; 01 "to" + "
D-day of the week, three English letters, such as:" Fri "
F-month, English full name, such as:" January "
h-12 hours, such as:" 01 "to" three "
H-24 hours of the hour; such as: "00" to "24"
g-12 hours, less than two does not fill 0, such as: "1" to "
G-two hours of the hour, the less than the non-does not fill 0, such as:" 0 "to" "
I-minute, such as:" 00 "to" 59 "
J-A few days, two digits, if less than two bits do not fill 0; such as: "1" to "+"
L-Day of the week, English full name, such as: "Friday"
M-month, two digits, if less than two digits in front of 0, such as: "01" to "three"
N-month, two digits, if less than two digits do not fill 0, e.g.: "1" To "Nine"
M-month, three English letters, such as: "Jan"
S-second, such as: "00" to "the"
S-the end of the English ordinal, two English letters, such as: "th", "nd"
T-Specify the number of days of the month, such as: "28" To "X"
U-Total seconds
W-Number of days of the week, such as: "0" (Sunday) to "6" (Saturday)
Y-year, four digits, such as: "1999"
Y-year, two digits; Day of the week; For example: "0" to "365"
other characters not listed above will list the character directly.
Example:

<?phpecho ("date () Example: <br/>"); Echo (Date ("L"). "<br/>");//l-Day of the week, English full name; For example: "Friday" Echo (Date ("L DS \of F Y h:i:s A"). "<br/>");//l week D date s English ordinal \of show of,f Y Month year, a on PM echo ("Oct 3,1975 is on a". Date ("L", Mktime (0,0,0,10,3,1975)). " <br/> "); Echo (Date (date_rfc822). "<br/>"); Echo (Date (Date_atom,mktime (0,0,0,10,3,1975)). "<br/><br/>"); Echo ("Gmdate () Greenwich Mean example: <br/>"); Echo (Gmdate ("L"). "<br/>"); Echo (Gmdate ("L DS \of F Y h:i:s A"). "<br/>"); Echo ("Oct 3,1975 is on a". Gmdate ("L", Mktime (0,0,0,10,3,1975)). " <br/> "); Echo (Gmdate (date_rfc822). "<br/>"); Echo (Gmdate (Date_atom,mktime (0,0,0,10,3,1975)). "<br/>");? >

Show:
Example of date ():

Tuesdaytuesday 23rd of June 01:26:19 Pmoct 3,1975 is on a fridaytue, June 13:26:19 +08001975-10-03t00:00:00+08: 00

Example of Gmdate () Greenwich Standard:

Tuesdaytuesday 23rd of June 05:26:19 Amoct 3,1975 is on a thursdaytue, June 05:26:19 +00001975-10-02t16:00:00+0 0:00

Six:
The getdate () function obtains the date/time information.
GETDATE (timestamp)
Timestamp optional Specifies the time in UNIX time format, not the current time
He returns a associative array with date information derived from timestamp. If the timestamp is not given, it is considered the current local time.

The cells in the array are as follows:
The number of "seconds" seconds represents 0 to 59
The number of "minutes" minutes represents 0 to 59
The number of "hours" hours represents 0 to 23
The number of days in the "Mday" month represents 1 to 31
The number of days in the "Wday" week represents 0 (representing Sunday) to 6 (for Saturday)
The number for the "Mon" month represents 1 to 12
The Year 4-digit number represents the full years for example: 1999 or 2003
"Yday" The number of days of the year represents 0 to 365
The full text of the "weekday" Day of the week represents Sunday to Saturday
Full text representation of "month" month January to December
0 The number of seconds since the Unix era began, similar to the return value of time () and the value used for date (). System-dependent, typical values are from 2147483648 to 2147483647.
Example:

<?phpprint_r (GETDATE ());? >

Show:

Array ([seconds] = 45[minutes] [52[hours] = 14[mday] [24[wday] = 2[mon] = 1[year] = 2006[yday ] [= 23[weekday] = Tuesday[month] = january[0] + 1138110765)

Example 2:

<?php$my_t=getdate (Date ("U"));p rint ("$my _t[weekday], $my _t[month] $my _t[mday], $my _t[year]");? >

Show:

Wednesday, January 25, 2006

Seven:
The gettimeofday () function returns an array containing the current time information.
The meaning of the returned array key is:
"SEC"-number of seconds since the Unix era
"USEC"-Number of microseconds
"Minuteswest"-the number of minutes to west of Greenwich
"Dsttime"-type of daylight saving time correction
Gettimeofday (Return_float)
Return_float is optional. When it is set to TRUE, Gettimeofday () returns a floating-point number.
Example 1

<?phpecho (Gettimeofday (True). "<br/><br/>");p Rint_r (Gettimeofday ());? >

Show:

1138111447.4
Array ([sec] = 1138111447[usec] = 395863[minuteswest] = -60[dsttime] + 0)

Example 2:

<?php$my_t=gettimeofday ();p rint ("$my _t[sec]. $my _t[usec]");? >

Show:

1138197006.988273

Eight:
The mktime () function returns a Unix timestamp for a date.
The gmmktime () function obtains a UNIX timestamp for the GMT date. Similar to Mktime (), the difference is that the return value is a time stamp of Greenwich Standard.
The parameter always represents the GMT date, so IS_DST has no effect on the result. As with Mktime (), parameters can be left-to-right and empty, and empty parameters are set to

The corresponding current GMT value.
Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST)
Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current Greenwich GMT value.
Hour is optional. Specified hours.
Minute is optional. Specify minutes.
Second is optional. Specify seconds.
Month is optional. Specifies the number of months to be represented.
Day is optional. Prescribed days.
Year is optional. Prescribed year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1.
Since 5.1.0, the IS_DST parameter has been discarded. Therefore, you should use the new Time zone processing feature.
Example
The Mktime () function is useful for date arithmetic and validation. It can automatically correct out-of-bounds input:

<?phpecho (Date ("M-d-y", Mktime (0,0,0,12,36,2001))), Echo (Date ("M-d-y", Mktime (0,0,0,14,1,2001))), Echo (Date (" M-d-y ", Mktime (0,0,0,1,1,2001)), Echo (Date (" M-d-y ", Mktime (0,0,0,1,1,99))); >

Output:

jan-05-2002feb-01-2002jan-01-2001jan-01-1999

Nine:
The strftime () function formats the local time/date according to the locale setting.
The gmstrftime () function formats the GMT/UTC time/date based on local locale settings. The same behavior as strftime (), the difference is that the return time is Greenwich Mean Time (GMT
)。
Strftime (Format,timestamp)
Format is optional. Specifies how the results are returned.
Timestamp is optional.
Example:

<?phpecho (Strftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98))), Echo (Gmstrftime ("%b%d%Y%x", Mktime (20,0,0,12,31,98)) );//Output The current date, time, and time zone echo ("It is%a on%b%d,%Y,%x gmstrftime zone:%Z", Times ());? >

Show:

DEC 1998 20:00:00DEC 1998 19:00:00it is Wed on Jan, 2006, 11:32:10 time zone:w. Europe Standard Time

Ten:
The idate () function formats the local time/date as an integer.
Unlike date (), Idate () accepts only one character as the format parameter.
Strftime (Format,timestamp)
Format is optional. Specifies how the results are returned.
B Swatch beat/internet Time
The day of the D-month
H Hours (12-hour format)
H hours (24-hour format)
I minute
I return 1 If daylight saving time is enabled, 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 since the Unix era (January 1 1970 00:00:00 GMT)-This works the same as time ()
The first day of the W Week (Sunday is 0)
The first week of the year in W ISO-8601 format, starting from Monday
Y year (1 or 2 digits – see instructions below)
Y year (4 digits)
The day ordinal of the Z-year
Z time zone offset in seconds
Timestamp is optional. The default value is the local current time, which is the value of times ().
Example:

<?phpecho (Idate ("Y"));? >

Show

2009

Eleven:
The localtime () function returns the local time (an array).
LocalTime (timestamp,is_associative) parameter description
Timestamp is optional. Specifies the date or time that is formatted. If timestamp is not specified, the current local time is used.
Is_associative is optional. Specifies whether to return an indexed array or an associative array.
The first parameter of LocalTime () is the timestamp and, if not given, the current time returned from a time ().
The second parameter is is_associative, which returns an ordinary numeric index array if set to false or not provided. If this argument is set to True then the localtime () function returns
Returns an associative array.
The different key names in the associative array are:
"Tm_sec"-Number of seconds
"Tm_min"-Number of minutes
"Tm_hour"-hours
"Tm_mday"-the day of the month
"Tm_mon"-the month ordinal of a year, starting from 0
"Tm_year"-year, starting from 1900
"Tm_wday"-the day of the week
"Tm_yday"-the day ordinal of a year
"TM_ISDST"-Daylight saving time is currently in effect
Note: The month from 0 (January) to 11 (December), the number of weeks from 0 (Sunday) to 6 (Saturday).
Example:

<?php$localtime = LocalTime (), $localtime _assoc = localtime (Time (), true);p Rint_r ($localtime);p Rint_r ($localtime _ Assoc);? >

Show:

Array (    [0] =    [1] = 3    [2] =    [3] = 3    [4] = 3    [5] =    [6] = 0    [7] =    + [9] = 1)

Eleven:
The microtime () function returns the current Unix timestamp and the number of microseconds.
Microtime (Get_as_float)
Get_as_float if the Get_as_float parameter is given and its value is equivalent to TRUE, the function returns a floating-point number.
If called without optional arguments, this function returns a string in the format "msec sec", where the SEC is from the Unix era (0:00:00 January 1, 1970 GMT)
Now the number of seconds, msec is the microsecond part. The two parts of a string are returned in seconds.
Example:

<?phpecho (Microtime ());? >

Show:

0.25139300 1138197510

Twelve:
The strptime () function resolves the date/time generated by strftime ().
Strptime (Date,format)
The string to parse for date (for example, returned from Strftime ()).
Format date uses the same formatting (as used in Strftime ()).
Strptime () returns an array after the date is parsed and returns FALSE if an error occurs.
The name of the month and day of the week and other language-related strings correspond to the current region (Lc_time) set by setlocale ().
The following cells are included in the array:
Key Name Description
Tm_sec number of seconds in the current minute (0-61)
Tm_min number of minutes in the current hour (0-59)
Tm_hour hours from midnight (0-23)
Day of the Month in Tm_mday (1-31)
Tm_mon has been a few months since January (0-11)
Tm_year has been a few years since 1900.
Tm_wday has been in the past few days since Sunday (0-6)
How many days Tm_yday this year since January 1 (0-365)
unparsed part of the date that was not recognized by the specified format
Example:

<php$format= "%d/%m/%y%h:%m:%s", $strf =strftime ($format), Echo ("$STRF");p Rint_r (Strptime ($STRF, $format)); >

Show:

03/10/2005 13:23:44array ([tm_sec] = 44[tm_min] + 23[tm_hour] [13[tm_mday] = 3[tm_mon] = 9[tm_year] =& Gt 105[tm_wday] = 0[tm_yday] = 276[unparsed] =)

13:
The date_sunrise () function returns the sunrise time for the specified date and place.
Date_sunrise (Timestamp,format,latitude,longitude,zenith,gmt_offset)
The date_sunset () function returns the sunset time for the specified date and place.
Date_sunset (Timestamp,format,latitude,longitude,zenith,gmt_offset)
1timestamp required.
2format is optional. Specify how the results are returned:
Sunfuncs_ret_string (returns the result in STRING format, such as 16:46)
Sunfuncs_ret_double (returns results in float format, e.g. 16.78243132)
Sunfuncs_ret_timestamp (returns the result in integer format (timestamp), e.g. 1095034606)
3latitude is optional. Latitude of the specified location. By default, north latitude is defined. Therefore, if you want to specify South latitude, you must pass a negative value.
4longitude is optional. Longitude of the specified location. By default, it refers to longitude. Therefore, if you want to specify West longitude, you must pass a negative value.
5zenith is optional.
6gmt_offset is optional. Specifies the difference between GMT and local time. The unit is the hour.
Example 1:

<?php//calculates sunrise time in Lisbon, Portugal//latitude: Latitude 38.4 degrees//longitude: Longitude 9 degrees West//zenith ~= 90//offset: +1 gmtecho ("Date:". Date ("D M D Y"). "<br/>"), Echo ("Sunrise Time:"), Echo (Date_sunrise (Time (), sunfuncs_ret_string,38.4,-9,90,1)); >

Show

Date:tue Jan 2006Sunrise time:08:52

Example 2:

<?php//calculate sunset Time in Lisbon, Portugal//latitude: Latitude 38.4 degrees//longitude: Longitude 9 degrees West//zenith ~= 90//offset: +1 gmtecho ("Date:". Date ("D M D Y"). "<br/>"), Echo ("Sunrise Time:"), Echo (Date_sunset (Time (), sunfuncs_ret_string,38.4,-9,90,1)); >

Show

Date:tue Jan 2006Sunrise time:18:44

14:
The strtotime () function resolves the datetime description of any English text to a Unix timestamp.
Strtotime (Time,now)
TIME specifies a string of times to parse.
Now is used to calculate the timestamp of the return value. If this argument is omitted, the current time is used.
The function expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (the number of seconds from January 1 1970 00:00:00 GMT) whose value
The current time of the system, if this parameter is not provided, relative to the time given by the now parameter.
The function uses the TZ environment variable, if any, to calculate the timestamp. Since PHP 5.1.0 There is an easier way to define a time zone for all date/time functions. This process
This is described in the Date_default_timezone_get () function page.
Example:

<?phpecho (Strtotime ("Now")), Echo (Strtotime ("3 October 2005"), Echo (Strtotime ("+5 hours")), Echo (Strtotime ("+1 Week "), Echo (Strtotime (" +1 Week 3 days 7 hours 5 Seconds ")), Echo (Strtotime (" Next Monday ")), Echo (Strtotime (" Last Sunday " ));? >

Show:

1138614504112829040011386325041139219304113950370911391804001138489200

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.