Date and time are usually used for display and conditional restrictions in programming languages. More specifically, you may want to display the time in a certain format, display the time in a certain time zone, get the time after one week, get the timestamp of the beginning of this week, convert the time in a certain format into another, etc. wait.
1. Display the current time
Use the date function, the format is date(format[, timestamp]), which accepts two parameters, The first parameter is a time format string, and the second parameter is a timestamp. The timestamp is optional. If not filled in, it will be the timestamp of the current time.
echo date('Y-m-d H:i:s'); // 2018-12-25 09:31:22
Like this, you can print out the current year, month, day, hour, minute and second. Of course, you can also print out the time in other formats depending on the format string:
echo date('l dS \of F Y h:i:s A'); // Tuesday 25th of December2018 09:34:54 AM
Explain what is used above Format characters:
Y Complete year, 4 digits
m Month with leading 0
d The day of the month, with a leading 0
H Hour in the 24-hour format, with a leading 0
i Minutes, with a leading 0
s Seconds, with leading 0
l The complete English version of the day of the week (note the lowercase L, not the uppercase i)
S The suffix of the number of days in the month (may be st, nd, rd, th)
F The complete English of the month
h 12-hour hour, with leading 0
A Morning or afternoon (AM or PM)
This is just a list For some of them, you can check the official manual for more complete format characters.
2. Display the world in a certain time zone
For example, by setting the time zone, we can get the time in the United States:
date_default_timezone_set("America/New_York"); echo date('Y-m-d H:i:s'); // 2018-12-24 20:54:36
The above is the world of New York, USA. You can check the PHP documentation for the string required by the time zone. If it is illegal, a warning will be generated. At the same time, the time zone will not be set successfully, but the default time zone will be used.
3. Calculated time
For example, the time at the beginning of this week, the beginning of this month, the time after one week, etc., you can Use the strtotime function to do this for us.
strtotime(time[, now = time()]) can convert any string describing time and date into a unix timestamp. The first parameter is the description string, and the second parameter is used to calculate timestamp (defaults to the current timestamp). You can tell from the description that it is a very flexible function, but be aware that it is a function after all. It is not so smart that it can understand "any" string describing time. It still needs a specific format - especially in China. , don’t expect it to understand what “December 25, 2018” means. But it is still very powerful and can do all the functions described previously.
echo '下个星期的时间:' . date('Y-m-d H:i:s', strtotime('+1 week')); // 下个星期的时间:2019-01-01 10:12:16 echo '本周开始时间:' . date('Y-m-d H:i:s', strtotime('this week Monday')); // 本周开始时间:2018-12-24 00:00:00 echo '明天开始时间:' . date('Y-m-d 00:00:00', strtotime('+1 day')); // 明天开始时间:2018-12-26 00:00:00 echo '1天2小时3分5秒之后的时间:' . date('Y-m-d H:i:s', strtotime('+1 day 2 hours 3 minutes 5 seconds')); // 1天2小时3分5秒之后的时间:2018-12-26 12:24:15
4. Create a time based on specific values
If you know the year, month, day, hour, minute, and second of a certain time , if you want to use them to create a time, you can choose to splice them into a time string and then use strtotime to parse it, or in this case you can also use a more reliable method: mktime.
mktime(hour, minute, second, month, day, year) It should be noted that the order of parameters is not the same as our habits, which is: hour, minute, second, month, day, year
$time = mktime(3, 10, 15, 2, 15, 2014); echo date('Y-m-d H:i:s', $time); // 2014-02-15 03:10:15
5. Time operations of object types
php also supports time operations of object types
$date_obj = date_create(); // 创建一个DateTime对象 echo $date_obj->format('Y-m-d H:i:s'); // 2018-12-25 10:45:08 date_add($date_obj, date_interval_create_from_date_string("3 days")); // 给对象增加3天 echo $date_obj->format('Y-m-d H:i:s'); // 2018-12-28 10:45:08 date_sub($date_obj, date_interval_create_from_date_string("2 days")); // 给对象减少2天 echo $date_obj->format('Y-m-d H:i:s'); // 2018-12-26 10:45:08 echo '时区为:' . timezone_name_get(date_timezone_get($date_obj)); // 时区为:PRC (中国时区,获取时区并打印)
It is different from the procedural style, but it can be implemented The functions are actually quite similar.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of PHP date and time operations. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools