Summary of the use of the date () function in PHP

Source: Internet
Author: User
Tags current time getdate local time locale locale setting lowercase readable string format

Format date
The first parameter of the date () function specifies how to format the date/time. It uses letters to indicate the date and time formats. Some available

Letter:

• Day in d-month (01-31)
• M-current month, in numbers (01-12)
• Y-current year (four digits)
You can find all the letters that can be used in the format parameters in our PHP Date reference manual.

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

The code is as follows: Copy code

<? Php
Echo date ("Y/m/d ");
Echo "<br/> ";
Echo date ("Y. m. d ");
Echo "<br/> ";
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

The code is as follows: Copy code

Echo date ('Y-m-J ');
2007-02-6

Echo date ('Y-n-J ');
07-2-6

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

The code is as follows: Copy code

Echo date ('Y-M-J ');
2007-Feb-6

Echo date ('Y-m-D ');
2007-02-06

Uppercase M indicates the three abbreviations of the month, while lowercase m indicates the number of the month (with leading 0 );
There is no upper-case J. Only lower-case j indicates the date of the month, and there is no leading o. If the month needs to have the upper-case J, the lower-case d is used.

The code is as follows: Copy code

Echo date ('Y-M-J ');
2007-Feb-6

Echo date ('Y-F-js ');
2007-February-6th

Uppercase M indicates the three abbreviated characters of the month, while uppercase F indicates the full English writing of the month. (No lower case f)
Uppercase S indicates the suffix of a date, such as "st", "nd", "rd", and "th". For details, see the date number.

Summary:
Y indicates that the year can be in uppercase or lowercase y;
Indicates that the month can be written in uppercase letters (F), uppercase letters (M), lowercase letters (m), and lowercase letters (n );
Indicates the date suffix.


2, hour: minute: Second

By default, PHP interprets the display time as "Greenwich Mean Time", which is 8 hours different from our local time.

The code is as follows: Copy code

Echo date ('G: I: s ');
5:56:57 am

Echo date ('H: I: s ');
05:56:57 AM

Lowercase g indicates the 12-hour system, with no leading 0, while lowercase h indicates the 12-hour system with leading 0.
When the 12-hour format is used, it indicates that upper afternoon, lower case a indicates lower case "am" and "pm", and upper case A indicates upper case "AM" and "PM ".

The code is as follows: Copy code

Echo date ('G: I: s ');
14:02:26

Uppercase G indicates the number of hours in the 24-hour format, but does not contain the leading value. Uppercase H indicates the number of hours in the 24-hour format.

Summary:
The letter g indicates that the hour does not contain a leading character, and the letter h indicates that the hour contains a leading character;
Lowercase g and h are in 12-hour format, while uppercase G and H are in 24-hour format.

3, leap year, week, day

The code is as follows: Copy code
Echo date ('L ');

This year's leap year: 0

The code is as follows: Copy code
Echo date ('L ');

Today is: Tuesday

The code is as follows: Copy code
Echo date ('D ');

Today is: Tue

Capital L indicates whether to determine the leap year of the year. If it is true, 1 is returned; otherwise, 0 is returned;
Lowercase l indicates the day of the week in full (Tuesday );
However, uppercase D is used to represent the three abbreviation of the day of the week (Tue ).

The code is as follows: Copy code
Echo date ('w ');

Today's week: 2

The code is as follows: Copy code
Echo date ('w ');

This week is the 06th week of the year.

Lowercase w indicates the day of the week, expressed in numbers
In upper case, W indicates the number of weeks in a year.

The code is as follows: Copy code
Echo date ('t ');

This month is 28 days

The code is as follows: Copy code
Echo date ('Z ');

Today is the 36th day of this year

Lowercase t indicates the number of days in the current month
Lowercase z indicates that today is the day of the year

4. Others

The code is as follows: Copy code
Echo date ('t ');
UTC

Uppercase T indicates the time zone setting of the server

The code is as follows: Copy code
Echo date ('I ');
0

If I is used to determine whether the current value is success, 1 is returned for true. Otherwise, 0 is returned.

The code is as follows: Copy code
Echo date ('u ');
1170769424

The upper-case u table shows the total number of seconds from January 1, January 1, 1970 to the present, which is the Unix timestamp of the UNIX time era.

The code is as follows: Copy code
Echo date ('C ');
2007-02-06T14: 24: 43 + 00: 00

Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, with the letter T to interval the date and time, the time format is HH: MM: SS, the time zone uses Greenway

The deviation of the Governance Standard Time (GMT.

The code is as follows: Copy code
Echo date ('r ');
Tue, 06 Feb 2007 14:25:52 + 0000

Lowercase r indicates the RFC822 date.


Add timestamp

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 a Unix timestamp for a specified date.

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

The day parameter is enough:

The code is as follows: Copy code

<? Php
$ Tomorrow = mktime (0, 0, 0, date ("m"), date ("d") + 1, date ("Y "));
Echo "tomorrow is". date ("Y/m/d", $ tomorrow );
?>

The output of the above code is similar to this:

Tomorrow is 2006/07/12

Some more advanced date and time functions are introduced to you.

This class will introduce more functions to enrich our applications.

The code is as follows: Copy code

Checkdate ($ month, $ date, $ year)

If the value of the application constitutes a valid date, the function returns true. For example, if the error date is January 1, February 31, 2005, this function returns false.

You can use this function to check the date and make it take effect before the date is used for calculation or storage in the database.

The code is as follows: Copy code

<? Php
// Returns false
Echo checkdate (2,30, 2005 )? "Valid": "invalid ";
// Returns true
Echo checkdate (2010 )? "Valid": "invalid ";
?>

Getdate ($ ts)

If no independent variable exists, this function returns the current date and time in combination with an array. Each element in the array represents one of the date/time values.

Specific components. You can submit optional time tag independent variables to the function to obtain the date/time value corresponding to the time tag.

Use this function to obtain a series of discrete and easily separated date/time values.

The code is as follows: Copy code


<? Php
// 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 is opposite to getdate (): it generates a UNIX time tag from a series of date and time values (GMT from January 1, January 1, 1970

The number of seconds that have elapsed ). When no independent variable is needed, it generates the UNIX time tag for the current time.

Use this function to obtain the UNIX time tag of real-time. This time label is usually used in many databases and programming languages.

The code is as follows: Copy code

<? Php
// Returns timestamp for 13:15:23 7-Jun-2006
Echo mktime );
?>

Date ($ format, $ ts)

This function format the UNIX time tag into a readable date string. 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 required string format.

Apply this function to display the formatting time or date.

The code is as follows: Copy code

<? Php
// Format current date
// Returns "13-Sep-2005 PM"
Echo date ("d-M-Y h: I A", mktime ());
?>

Strtotime ($ str)

This function converts readable English date/time strings into UNIX time labels.

Use this function to convert non-standard date/time strings into standard and compatible UNIX time labels.

The code is as follows: Copy code

<? Php
// Returns 13-Sep-05
Echo date ("d-M-y", strtotime ("today "));
// Returns 14-Sep-05
Echo date ("d-M-y", strtotime ("tomorrow "));
// Returns 16-Sep-05
Echo date ("d-M-y", strtotime ("today + 3 days "));
?>

Strftime ($ format, $ ts)

As defined by the setlocale () function, this function formats the UNIX time tag to a date string applicable to the current environment.

Use this function to create a date string compatible with the current environment.

The code is as follows: Copy code

<? Php
// 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: % ");
?>

 

Microtime ()

As defined by the setlocale () function, this function formats the UNIX time tag to a date string applicable to the current environment.

Use this function to create a date string compatible with the current environment.

The code is as follows: Copy code


<? Php
// 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 execution
Echo "Elapsed time:". ($ end-$ start). "sec ";
?>

 

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

This function generates a UNIX time tag by a series of date and time values expressed in GMT. When no independent variable is needed, it generates the current GMT instant time

UNIX time label.

This function is used to obtain UNIX time tags of GMT real-time.

The code is as follows: Copy code


<? Php
// Returns timestamp for 12:25:23 9-Jul-2006
Echo gmmktime );
?>

 

Gmdate ($ format, $ ts)

This function formats UNIX time tags into readable date strings. This date string is expressed in GMT (non-local time.

This function is applied when the time tag is expressed in GMT.

The code is as follows: Copy code


<? Php
// Format current date into GMT
// Returns "13-Sep-2005 08:32 AM"
Echo gmdate ("d-M-Y h: I A", mktime ());
?>

Date_default_timezone_set ($ tz), date_default_timezone_get ()

All date/time function calls after this function 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 future time operations.

The code is as follows: Copy code


<? Php
// Set timezone to UTC
Date_default_timezone_set ('utc ');
?>

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.