Table of Contents
1. Display the current time" >1. Display the current time
2. Display the world in a certain time zone" >2. Display the world in a certain time zone
3. Calculated time" >3. Calculated time
4. Create a time based on specific values" >4. Create a time based on specific values
5. Time operations of object types" >5. Time operations of object types
Home Backend Development PHP Tutorial PHP date and time operations

PHP date and time operations

Oct 14, 2019 pm 04:52 PM
date Timestamp

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 (中国时区,获取时区并打印)
Copy after login

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!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

1. Let me first talk about the method I used at the beginning, maybe everyone is using it too. First, open [View]——]Remarks Template[. 2. A place where you can actually see the date after opening it. 3. Select it first and delete it. 4. After deleting, click [Close Master View]. 5. Open the print preview again and find that the date is still there. 6. In fact, this date was not deleted here. It should be in the [Handout Master]. Look at the picture below. 7. Delete the date after you find it. 8. Now when you open the preview and take a look, the date is no longer there. Note: In fact, this method is also very easy to remember, because the printed handouts are handouts, so you should look for the [Handout Master].

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

How to generate k random dates between two dates using Python? How to generate k random dates between two dates using Python? Sep 09, 2023 pm 08:17 PM

Generating random data is very important in the field of data science. From building neural network predictions, stock market data, etc., date is usually used as one of the parameters. We may need to generate random numbers between two dates for statistical analysis. This article will show how to generate k random dates between two given dates using the random and datetime modules. Datetime is Python’s built-in library for handling time. On the other hand, the random module helps in generating random numbers. So we can combine random and datetime modules to generate a random date between two dates. Syntax random.randint (start, end, k) random here refers to the Python random library. The randint method uses three important

How to display date and seconds in the top bar of Ubuntu 17.10? How to display date and seconds in the top bar of Ubuntu 17.10? Jan 08, 2024 am 10:41 AM

By default, the top bar of Ubuntu 17.10 only has the current time and no date. What should I do if I want to display the date? Let’s take a look at the detailed tutorial below. 1. Open the terminal in the launcher, or press [Ctrl+Alt+T] 2. Enter in the terminal: sudoaptinstallgnome-tweak-tool 3. After the installation is completed, open the tweak tool 4. Click TopBar 5. Date is the date and seconds is the number of seconds 6. After setting it up, the date and seconds will be displayed on the time in the top bar.

How to change the date into a pound sign in Excel How to change the date into a pound sign in Excel Mar 20, 2024 am 11:46 AM

Excel software has very powerful data processing functions. We often use excel software to process various data. Sometimes when we enter a date in an excel cell, the date in excel changes to a pound sign. How can we display the data normally? Let’s take a look at the solution below. 1. First, we put the mouse on the column width line between columns AB, double-click and adjust the column width, as shown in the figure below. 2. After the column is widened, we find that numbers are displayed in the cells instead of dates. This is definitely incorrect. Then we should check the format of the cells, as shown in the figure below. 3. Click the "Number" option in the "Home" tab, and click "Other Number Format" in the drop-down menu, as shown in the figure below.

Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Nov 03, 2023 am 09:30 AM

Java document interpretation: Usage analysis of the currentTimeMillis() method of the System class, specific code examples are required. In Java programming, the System class is a very important class, which encapsulates some properties and operations related to the system. Among them, the currentTimeMillis method is a very commonly used method in the System class. This article will explain the method in detail and provide code examples. 1. Overview of currentTimeMillis method

How to use the time and date modules in Python How to use the time and date modules in Python Oct 16, 2023 am 08:11 AM

How to use the time and date modules in Python Introduction: In programming, dealing with time and dates are very common tasks. Python provides powerful time and date modules, making time and date operations easier and more convenient. This article will introduce the time and date modules in Python and provide specific code examples to help readers better understand and apply them. 1. Introducing the time and date module Python’s built-in time and date module is the datetime module. We need to introduce this module first.

See all articles