search
HomeBackend DevelopmentPHP TutorialSummary of usage of date and time function date() in PHP_PHP tutorial

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 $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 $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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor