Home  >  Article  >  Backend Development  >  Summary of usage of date and time function date() in PHP_PHP tutorial

Summary of usage of date and time function date() in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:221239browse

date() is a commonly used date and time function. Let me summarize the various forms of usage of the date() function. Friends who need to learn can refer to it. ​

Format Date
The first parameter of the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some available

Letters of

:

•d - day of the month (01-31)
•m - Current month as a number (01-12)
•Y - Current year (four digits)
You can find all the letters that can be used in the format parameter in our PHP Date reference manual.

You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formatting:

The code is as follows Copy code
 代码如下 复制代码

echo date("Y/m/d");
echo "
";
echo date("Y.m.d");
echo "
";
echo date("Y-m-d");
?>

echo date("Y/m/d");

echo "

";

echo date("Y.m.d");
echo "
";

echo date("Y-m-d");

?>

The output of the above code is similar to this:
 代码如下 复制代码

echo date('Y-m-j');
2007-02-6

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

2006/07/11
2006.07.11

2006-07-11
 代码如下 复制代码

echo date('Y-M-j');
2007-Feb-6

echo date('Y-m-d');
2007-02-06


1. Year-month-day

The code is as follows Copy code
 代码如下 复制代码

echo date('Y-M-j');
2007-Feb-6

echo date('Y-F-jS');
2007-February-6th

echo date('Y-m-j');

2007-02-6

echo date('y-n-j');

07-2-6


Capital Y represents the four-digit year, while lowercase y represents the two-digit year;
Lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.
The code is as follows Copy code

echo date('Y-M-j');
2007-Feb-6

echo date('Y-m-d');

2007-02-06
 代码如下 复制代码

echo date('g:i:s a');
5:56:57 am

echo date('h:i:s A');
05:56:57 AM

Capital M represents the 3 abbreviated characters of the month, while lowercase m represents the number of the month (with leading 0); There is no uppercase J, only lowercase j represents the day of the month, without the leading o; if a leading month is required, use a lowercase d.
The code is as follows Copy code
echo date('Y-M-j'); 2007-Feb-6 echo date('Y-F-jS'); 2007-February-6th
Capital M represents the 3 abbreviated characters of the month, while capital F represents the full English character of the month. (no lowercase f) Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number. Summary: You can use uppercase Y or lowercase y to indicate the year; Months can be represented by uppercase F, uppercase M, lowercase m and lowercase n (two ways to represent characters and numbers respectively); Lowercase d and lowercase j can be used to represent the day, and uppercase S represents the suffix of the date. 2. Hour:minute:second By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.
The code is as follows Copy code
echo date('g:i:s a'); 5:56:57 am echo date('h:i:s A'); 05:56:57 AM

A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".

The code is as follows Copy code
 代码如下 复制代码

echo date('G:i:s');
14:02:26

echo date('G:i:s');

14:02:26

Capital G represents the hour in 24-hour format, but without leading; use capital H to represent the hour in 24-hour format with leading

Summary:

The letter g represents the hour without leading, and the letter h represents the hour with leading;

Lowercase g and h represent the 12-hour format, while uppercase G and H represent the 24-hour format.

 代码如下 复制代码
echo date('L');
3, leap year, week, day

 代码如下 复制代码
echo date('l');
Whether this year is a leap year: 0

 代码如下 复制代码
echo date('D');
Today is: Tuesday

Today is: Tue

Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;

The lowercase l represents the full English word for the day of the week (Tuesday);
 代码如下 复制代码
echo date('w');
Instead, use a capital D to represent the 3-character abbreviation of the day of the week (Tue).

 代码如下 复制代码
echo date('W');
Today’s week: 2

This week is week 06 of the year

The lowercase w represents the day of the week, and the number represents it
 代码如下 复制代码
echo date('t');
Capital W represents the number of weeks in the year

This month is 28 days

 代码如下 复制代码
echo date('z');

Today is the 36th day of the year

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

4, other

The code is as follows Copy code
echo date('T');
 代码如下 复制代码
echo date('T');
UTC
UTC

Capital T indicates the server’s time locale

The code is as follows Copy code
echo date('I');
 代码如下 复制代码
echo date('I');
0
0

Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0

The code is as follows Copy code
echo date('U');
 代码如下 复制代码
echo date('U');
1170769424
1170769424

Capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.

The code is as follows Copy code
echo date('c');
 代码如下 复制代码
echo date('c');
2007-02-06T14:24:43+00:00
2007-02-06T14:24:43+00:00

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

Expressed by the deviation from GMT.

The code is as follows Copy code
echo date('r');
 代码如下 复制代码
echo date('r');
Tue, 06 Feb 2007 14:25:52 +0000
Tue, 06 Feb 2007 14:25:52 +0000

Lowercase r indicates 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.

Grammar
mktime(hour,minute,second,month,day,year,is_dst) If we need to get the timestamp of a certain day, we only need to set the

of the mktime() function

day parameter will do:

The code is as follows Copy code
 代码如下 复制代码

$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "明天是 ".date("Y/m/d", $tomorrow);
?>

$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

There are also some more advanced date and time functions to introduce to you

This category will introduce more functions to enrich our applications.

The code is as follows Copy code
 代码如下 复制代码

checkdate($month,$date,$year)

checkdate($month,$date,$year)

This function returns true if the applied value forms a valid date. For example, for the error date February 31, 2005, this function returns false.

This function can be used to check and validate a date before it is used in calculations or saved in the database.
 代码如下 复制代码

// returns false
echo checkdate(2,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>

getdate($ts)

The code is as follows Copy code

// returns false
echo checkdate(2,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>

getdate($ts)

With no arguments, this function returns the current date and time in a combined array. Each element in the array represents one of the 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)

Specific components. An optional time stamp argument can be submitted to the function to obtain a date/time value corresponding to the time stamp.

Apply this function to obtain a series of discrete, easily separable date/time values.

The code is as follows Copy code

// 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)
 代码如下 复制代码

// returns timestamp for 13:15:23 7-Jun-2006
echo mktime(13,15,23,6,7,2006);
?>

 date($format, $ts)

This function does the opposite of getdate(): it generates a UNIX time stamp from a series of date and time values ​​(GMT time January 1, 1970 to

Number of seconds elapsed now). When no arguments are used, it generates a UNIX time stamp of the current time.

Use this function to obtain the UNIX time label of the immediate time. Such timestamps are commonly used in many databases and programming languages.

The code is as follows Copy code
 代码如下 复制代码

// format current date
// returns "13-Sep-2005 01:16 PM"
echo date("d-M-Y h:i A", mktime());
?>

 strtotime($str)

// returns timestamp for 13:15:23 7-Jun-2006
echo mktime(13,15,23,6,7,2006);
?> date($format, $ts)
This function formats a UNIX time stamp into a human-readable date string. It is the most powerful function in the PHP date/time API and can be used in Convert the integer time label into the required string format in a series of correction values. Apply this function when formatting time or date for display.
The code is as follows Copy code
// format current date
// returns "13-Sep-2005 01:16 PM"
echo date("d-M-Y h:i A", mktime());
?> strtotime($str)

This function converts a human-readable English date/time string into a UNIX time tag.

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

The code is as follows Copy code
 代码如下 复制代码

// 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)

// 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 previous setlocale() function, this function formats the UNIX time stamp into a date string suitable for 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 ");
?>

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

The code is as follows Copy code

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

 代码如下 复制代码


// 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";
?>

microtime()

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

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

The code is as follows Copy code

// get starting value
$start = microtime();

// run some code
for ($x=0; $x<1000; $x++) {
$null = $x * $x;
}
代码如下 复制代码


// returns timestamp for 12:25:23 9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?>

 

// get ending value
$end = microtime();

// calculate time taken for code execution
echo "Elapsed time: " . ($end - $start) ." sec";
?>

 代码如下 复制代码


// format current date into GMT
// returns "13-Sep-2005 08:32 AM"
echo gmdate("d-M-Y h:i A", mktime());
?>

gmmktime($hour, $minute, $second, $month, $day, $year) This function generates a UNIX time stamp from a series of date and time values ​​expressed in GMT time. When no independent variable is used, it generates a current GMT real-time time UNIX time tag for . Use this function to obtain the UNIX time label of GMT real-time time.
The code is as follows Copy code

// returns timestamp for 12:25:23 9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?>
gmdate($format, $ts) This function formats a UNIX time stamp into a human-readable date string. This date string is expressed in GMT (not local time). Apply this function when using GMT to represent the time label.
The code is as follows Copy code

// 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 set and restore the default time zone.

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

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

The code is as follows Copy code
 代码如下 复制代码


// set timezone to UTC
date_default_timezone_set('UTC');
?>


// set timezone to UTC
date_default_timezone_set('UTC');
?>

http://www.bkjia.com/PHPjc/445289.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445289.htmlTechArticledate() is a commonly used date and time function. Let me summarize the various aspects of the date() function. Friends who need to learn how to use this form can refer to it. Format date date() function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn