


Example introduction to PHP date function date format conversion_PHP tutorial
When developing websites, we often need to process dates and times. PHP provides many date and time functions to facilitate PHP developers to calculate and format dates and times. Therefore, it is very necessary to master PHP date functions, and it also lays the foundation for processing the format conversion between PHP date functions and Mysql database dates. Here we use PHP examples to introduce the basics of PHP: date function date and Unix timestamp function, as well as methods of converting date formats between each other.
PHP date and time zone setting
Before enabling the PHP date function, you first need to ensure that the set time zone is correct, otherwise the displayed date may be different. When setting up the PHP environment, you only need to set date.timezone to the corresponding time zone in the PHP.INI configuration file. If you do not have the right to operate the PHP.INI configuration file, you can obtain the time zone of the current PHP running environment through the PHP time zone function date_default_timezone_get(void), and then use the date_default_timezone_set(string $timezone_identifier) function to set the corresponding time zone. More time zones supported by PHP You can check http://www.php.net/manual/en/timezones.php.
PHP format date function Date
Prototype string date(string $format[,int $timestamp])
$format - format code of PHP date function date
$timestamp - Unix timestamp, defaults to current timestamp (optional)
Format character———- Description———— Return value description
Day
1
d ——The day of the month, returns 2 digits ——01 to 31
2
j ——The day of the month, 1 to 9 without zero ——1 to 31
3
S - English suffix for the number of days in the month - 2 characters st, nd, rd or th. Can be used with j
4
z ——The day of the year ——0 to 366
Week
1
l —— day of the week ——Sunday to Saturday
2
D - English day of the week, text representation - 3 letters Mon to Sun
3
N - represents the day of the week as a number (new in PHP 5.1.0) - 1 (for Monday) to 7 (for Sunday)
4
w ——The day of the week ——0 (indicating Sunday) to 6 (indicating Saturday)
5
W - Week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) - 42 (the 42nd week of the year)
Month
1
F ——month ——January or March January to December
2
m ——The month represented by the number ——01 to 12
3
M - three-letter abbreviation for the month - Jan to Dec
4
n ——The month represented by the number ——1 to 12
5
t —– Number of days in the month — 28 to 31
Year
1
L - Leap year, expressed as a Boolean value - 1 if it is a leap year, otherwise 0
2
o ——Year number in ISO-8601 format. Similar to Y, unless the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0)——1999 or 2003
3
Y —— The year expressed as a four-digit number ——1999 or 2003
4
y - two-digit year - 99 or 03
Time
1
a —— morning and afternoon ——am or pm
2
A —— Morning and Afternoon ——AM or PM
3
B ——Swatch Internet time ——000 to 999
4
g - hour, 12 hour format - no leading zeros 1 to 12
5
G - hour, 24 hour format - no leading zeros 0 to 23
6
h - hour, 12 hour format - with leading zeros 01 to 12
7
H - hour, 24 hour format - with leading zeros 00 to 23
8
i —— minutes —— 00 to 59
9
s —— seconds —— 00 to 59
Timezone
1
e - Time zone identifier (new in PHP 5.1.0) - UTC, GMT, Atlantic/Azores
2
I —— Whether it is daylight saving time —— Daylight saving time is 1, otherwise it is 0
3
T — Time zone abbreviation — for example: EST, MDT
4
Z —— Time zone difference from the current time zone, in seconds —— -43200 to 43200
Full Date/Time
1
c ——Date in ISO 8601 format (new in PHP 5) ——2004-02-12T15:19:21+00:00
2
r — Date in RFC 822 format — Thu, 21 Dec 2000 16:01:07 +0200
3
U——General description from 1970 1.1 to a certain moment, that is, Unix timestamp, see time()
For the English documentation of PHP date function date formatting characters, please refer to http://www.php.net/manual/en/function.date.php
Tips for using the date function: When using the PHP date function date to output a formatted date format, you can first write out the date format you need to output, and then use the formatting characters in the date function to combine it. It will be very easy to use. Handy.
Use PHP date function date to format Unix timestamp
The second optional parameter $timestamp of the PHP date function exists in the form of a Unix timestamp. Through this parameter, the Unix timestamp can be converted into the corresponding date format. Using Unix timestamp has many benefits in parameter passing, date calculation, etc. It is convenient and concise.
Commonly used functions in PHP to convert dates into Unix timestamps include mktime, time, and strtotime.
The PHP date function mktime is mainly used to convert date and time into Unix timestamp format.
The prototype is as follows
1
int mktime ([int $hour = date("H")
2
[,int $minute = date("i")
3
[,int $second = date("s")
4
[,int $month = date("n")
5
[,int $day = date("j")
6
[,int $year = date("Y")
7
[,int $is_dst = -1
8
]]]]]]])
Note: All parameters of the mktime function are optional, where $is_dst indicates whether it is daylight saving time. When the parameters of the mktime function are empty, it will default to the current time, similar to the time function.
The PHP date function time is mainly used to obtain the current time, which exists in Unix timestamp format.
The PHP date function strtotime is mainly used to convert the date and time described in English into a Unix timestamp.
The prototype is as follows
1
int strtotime(string $time[,int $now])
Note: The $time parameter mainly exists in the form of English text description.
The $now optional parameter is mainly expressed as a basis for $time to calculate date and time, and the default is the current Unix timestamp.
The PHP date function is mainly used to convert and output various date formats. If you want to obtain various parts of the date and time, you can obtain it by passing the Unix timestamp parameter to the getdate function. The getdate function will return a related array with the array key values as follows:
01
seconds —- Number of seconds
02
minutes —- minutes
03
hours —- hours
04
mday —- Date in the month
05
wday —- Day of the week, 0 (meaning Sunday) to 6 (meaning Saturday)
06
mon —- month
07
year —- The complete year represented by four digits
08
yday —- the day of the year
09
weekday —- Text representation of the week, Sunday to Saturday
10
month —- Month text representation, January to December
11
0 —- The number of seconds since the Unix epoch, similar to the return value of time().
Through the above PHP Unix timestamp functions, combined with the PHP date function, various date format conversion, calculation and output can be performed.
PHP date calculation example:
Calculate age
01
02
$day = 1;
03
$month = 1;
04
$year = 1989;
05
06
$birthday = mktime(0,0,0,$month,$day,$year);
07
08
$nowunix = time();
09
10
$age = $nowunix - $birthday;
11
12
$age = floor($age / (365*24*60*60));
13
14
echo $age;
15
?>
Date addition and subtraction
01
02
$nowUnix = time();
03
04
$addTime = $nowUnix + (24*60*60); //Add one day to the date
05
06
$redTime = $nowUnix - (24*60*60);//Date minus one day
07
08
echo date("Y-m-j",$addTime);
09
echo date("Y-m-j",$redTime);
10
?>
Example of strtotime date calculation and format conversion
Date addition and subtraction can also be completed through the strtotime function
01
02
$nowTime = strtotime("now");
03
echo date("Y-m-j",$nowTime)."
";
04
05
$nowTime = strtotime("10 September 2010");
06
echo date("Y-m-j",$nowTime)."
";
07
08
$nowTime = strtotime("+1 day");
09
echo date("Y-m-j",$nowTime)."
";
10
11
$nowTime = strtotime("+1 week");
12
echo date("Y-m-j",$nowTime)."
";
13
14
$nowTime = strtotime("+1 week 2 days 4 hours 2 seconds");
15
echo date("Y-m-j",$nowTime)."
";
16
17
$nowTime = strtotime("next Thursday");
18
echo date("Y-m-j",$nowTime)."
";
19
20
$nowTime = strtotime("last Monday");
21
echo date("Y-m-j",$nowTime)."
";
22
?>
至此,PHP日期函数date格式化字符含义以及部分PHP Unix时间戳函数的介绍以及相互间的转换和格式化输出实例就介绍完了,这对掌握PHP日期、时间函数的计算、与转换是非常必要的。希望对PHP入门学习的朋友有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
