A summary of date and time functions in PHP usage dates ()

Source: Internet
Author: User
Tags add time echo date local time locale locale setting string format

[Introduction] date () is a commonly used date-time function, let me summarize the date () function of the various forms of usage, there is a need to learn the friend can refer to. The first parameter of the format date () function specifies how datetime is formatted. It uses letters to represent dates and times

Formatted date
The first parameter of the date () function specifies how the date/time is formatted. It uses letters to represent the format of dates and times. Some of the available letters are listed here:

D-Day of the month (01-31)
• M-Current month, in Digital (01-12)
Y-Current year (four digits)
You can find all the letters you can use in the format parameters in our PHP Date reference manual.

You can insert additional characters between letters, such as "/", ".", or "-" to add additional formatting:

echo Date ("y/m/d"""; echo Date ("y.m.d"""; echo Date ("y-m-d");? >

The output of the above code is similar to this:

2006/07/11
2006.07.11
2006-07-11


1, year-month-day

echo Date ('y-m-j'); -6echo date ('y-n-j');  -2-6

Uppercase Y represents the year four digits, while lowercase y represents the two digits of the year;
Lowercase m represents the number of months (with leading), while lowercase n indicates no leading month number.

echo Date ('y-m-j'); -feb-6echo date ('y-m-d'); -Geneva-

The uppercase M represents the 3 abbreviated characters of the month, while the lowercase m represents the number of the month (with leading 0);
J with no uppercase, only lowercase J indicates the date of the month, no leading o, or lowercase D if a month with a leading is required.

Echo Date (' Y-m-j '); 2007-feb-6echodate(' Y-f-js '); 2007-february-6th

Uppercase M represents the 3 abbreviated characters of the month, while uppercase F indicates the full written English of the month. (No lowercase f)
Uppercase S represents the suffix of the date, such as "St", "nd", "rd", and "th", depending on the date number.

Summary:
Indicates that the year can be in uppercase Y and lowercase y;
Indicates that the month can be in uppercase F, uppercase M, lowercase m, and lowercase n (two ways of representing characters and numbers, respectively);
Indicates that the day can be in lowercase d and lowercase j, and uppercase s represents the suffix of the date.

2, Hours: minutes: seconds

By default, the PHP interpretation is displayed as "Greenwich Mean Time", which differs from our local time by 8 hours.

Echo Date (' G:i:s a '); 5:56:57 amechodate(' h:i:s A '); 05:56:57 AM

The lowercase g represents a 12-hour system without a leading 0, while the lowercase h indicates a 12-hour system with a leading 0.
When using the 12-hour system, it is necessary to indicate that in the afternoon, lowercase a represents the lowercase "am" and "PM", and uppercase a denotes uppercase "AM" and "PM".

Echo Date (' g:i:s '); 14:02:26

Uppercase G denotes 24-hour hours, but without preamble, and uppercase H for leading 24-hour hours

Summary:
The letter g means the hour without leading, the letter H denotes the hour with the preamble;
lowercase g, h for 12-hour system, uppercase G, h for 24-hour system.

3, Leap year, week, day

Echo Date (' L ');

Leap Year: 0

Echo Date (' l ');

Today is: Tuesday

Echo Date (' D ');

Today is: Tue

Uppercase L Indicates whether this year is a leap years, a Boolean value that returns 1 for true, otherwise 0;
Lowercase L Indicates the day of the week in English full write (Tuesday);
Instead, use uppercase D to denote the 3-character abbreviation (TUE) of the day of the week.

Echo Date (' W ');

Today's Week: 2

Echo Date (' W ');

This week is the No. 06 week of the year.

The lowercase w represents the day of the week, and the numeric representation
Uppercase W indicates the number of weeks in a year

Echo Date (' t ');

This month is 28 days

Echo Date (' z ');

Today is the 36th day of the year.

Lowercase t indicates the current month and how many days
Lowercase z indicates that today is the first day of the year

4, other

Echo Date (' T '); UTC

Capital T indicates the server's time zone setting

Echo Date (' I '); 0

Uppercase I indicates whether the current is daylight saving time, returns 1 for true, otherwise 0

Echo Date (' U '); 1170769424

The capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp for the Unix time era.

Echo Date (' C '); 2007-02-06t14:24:43+00:00

The lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, the letter T is used to interval the date and time, the time format is HH:MM:SS, and the timezone is used Greenway

Deviations from the standard Time (GMT).

Echo Date (' R '); Tue, Feb 2007 14:25:52 +0000

The lowercase R represents the RFC822 date.


Add Time stamp

The second parameter of the date () function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.

In our example, we will use the Mktime () function to create a timestamp for tomorrow.

The Mktime () function returns the Unix timestamp for the specified date.

Grammar
Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,IS_DST) to get a timestamp for a given day, we just set the Mktime () function's

The day parameter is available:

$tomorrow Mktime (0,0,0,date("M"),date("D") +1,date("Y")); Echo "Tomorrow is." Date $tomorrow );? >

The output of the above code is similar to this:

Tomorrow is 2006/07/12.

There are some more advanced date-time functions introduced to everyone

This class will introduce more functions to enrich our application.

checkdate ($month,$date,$year)

If the applied value constitutes a valid date, the function returns to true. For example, for the error date February 31, 2005, this function returns FALSE.

Use this function to check the date and make the date effective before the date is used to calculate or save in the database.

// returns false Echo checkdate (2,30,2005)? "Valid": "Invalid"; // returns True Echo checkdate (4,6,2010)? "Valid": "Invalid";? >getdate($ts)

In the absence of an argument, the function returns the current date and time in the form of an array. Each element in the array represents one of the date/time values

Specific components. You can submit an optional time label argument to the function to get the date/time value that corresponds to the time label.

Apply this function to obtain a series of discrete, easy-to-detach date/time values.

//get date as associative array$arr=getdate();Echo"Date is".$arr[' Mday ']. " " .$arr[' Weekday ']. " " .$arr[' Year '];Echo"Time is".$arr[' hours ']. ":" .$arr[' Minutes '];?>Mktime($hour,$minute,$second,$month,$day,$year)

This function has the opposite effect of getdate (): It generates a UNIX time label from a series of date and time values (GMT time January 1, 1970 to

The number of seconds elapsed now). When no arguments are used, it generates the UNIX time label for the current time.

Use this function to get the UNIX time label for immediate time. This time label is often used in many database and program languages.

// returns timestamp for 13:15:23 7-jun-2006 Echo Mktime (13,15,23,6,7,2006); Date ($format$ts)

This function formats the UNIX time label as a date string that can be artificially read. It is the most powerful function in the PHP date/Time API and can be used in

In a series of correction values, the integer time label is converted to the desired string format.

This function is applied when the time or date is formatted for display.

// format Current date//returns "13-sep-2005 01:16 PM" Echo Date Mktime ()); Strtotime ($str)

This function converts a human-readable English date/time string into a Unix time label.

Apply this function to convert a non-normalized date/time string into a standard, compatible UNIX time label.

//returns 13-sep-05Echo Date("D-m-y",Strtotime("Today"));//returns 14-sep-05Echo Date("D-m-y",Strtotime("Tomorrow"));//returns 16-sep-05Echo Date("D-m-y",Strtotime("Today +3 Days"));?>strftime($format,$ts)

As defined by the previous setlocale () function, this function formats the UNIX time label as a date string for the current environment.

Apply this function to establish a date string that is compatible with the current environment.

// set Locale to France (on Windows) setlocale (Lc_time, "Fra_fra"); // format Month/day names//as per locale setting//returns "septembre" and "mardi" Echo strftime ("Month:%B"); Echo strftime ("Day:%A");? >

Microtime ()

As defined by the previous setlocale () function, this function formats the UNIX time label as a date string for the current environment.

Apply this function to establish a date string that is compatible with the current environment.

//Get starting value$start=Microtime();//Run some code for($x= 0;$x<1000;$x++) {$null=$x*$x;}//Get ending value$end=Microtime();//calculate time taken for code executionEcho"Elapsed Time:". ($end-$start). "SEC";?>

Gmmktime ($hour, $minute, $second, $month, $day, $year)

This function generates a UNIX time label from a series of date and time values expressed in GMT time. When no arguments are used, it generates a current GMT instantaneous time

The Unix time label.

Use this function to get the UNIX time label for the GMT instant time.

// returns timestamp for 12:25:23 9-jul-2006 Echo Gmmktime (12,25,23,7,9,2006);? >

Gmdate ($format, $ts)

This function formats the UNIX time label as a date string that can be read artificially. This date string is expressed in GMT (non-local time).

This function is applied when the time label is represented in GMT.

// format current date into gmt//returns "13-sep-2005 08:32 AM" Echo gmdate Mktime ());? >

Date_default_timezone_set ($tz), Date_default_timezone_get ()

This function then all date/Time function calls are set and the default time zone is restored.

Note: This function is only valid in PHP 5.1+.

This function is a convenient shortcut for setting the time zone for future operations.

// set TimeZone to UTCDate_default_timezone_set (' UTC ');? >

A summary of date and time functions in PHP usage dates ()

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.