


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
:
•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 " ";
echo date("Y.m.d");
?> |
代码如下 | 复制代码 |
echo date('Y-m-j'); echo date('y-n-j'); |
2006/07/11
2006.07.11
代码如下 | 复制代码 |
echo date('Y-M-j'); echo date('Y-m-d'); |
1. Year-month-day
The code is as follows | Copy code | ||||
2007-02-6
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'); echo date('Y-m-d'); 2007-02-06
|
The code is as follows | Copy code |
echo date('Y-M-j'); 2007-Feb-6 echo date('Y-F-jS'); 2007-February-6th |
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 | ||||
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:
Lowercase g and h represent the 12-hour format, while uppercase G and H represent the 24-hour format.
代码如下 | 复制代码 |
echo date('L'); |
代码如下 | 复制代码 |
echo date('l'); |
代码如下 | 复制代码 |
echo date('D'); |
Today is: Tue
Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
代码如下 | 复制代码 |
echo date('w'); |
代码如下 | 复制代码 |
echo date('W'); |
This week is week 06 of the year
代码如下 | 复制代码 |
echo date('t'); |
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');
|
Capital T indicates the server’s time locale
The code is as follows | Copy code | ||||
echo date('I');
|
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');
|
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');
|
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');
|
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
day parameter will do:
The code is as follows | Copy code | ||||
|
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 | ||||
|
This function can be used to check and validate a date before it is used in calculations or saved in the database.
代码如下 | 复制代码 |
// returns false getdate($ts) |
The code is as follows | Copy code |
// returns false 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
代码如下 | 复制代码 |
mktime($hour, $minute, $second, $month, $day, $year) |
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)
|
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 | ||||
echo mktime(13,15,23,6,7,2006); ?> date($format, $ts) |
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 | ||||
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)
|
代码如下 | 复制代码 |
// set locale to France (on Windows) // format month/day names echo strftime("Month: %B "); |
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) // format month/day names echo strftime("Month: %B "); |
代码如下 | 复制代码 |
// run some code // get ending value // calculate time taken for code execution |
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; }
// get ending value // calculate time taken for code execution |
代码如下 | 复制代码 |
|
The code is as follows | Copy code |
// returns timestamp for 12:25:23 9-Jul-2006 echo gmmktime(12,25,23,7,9,2006); ?> |
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'); ?> |

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
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
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
