Home Backend Development PHP Tutorial php development time

php development time

Aug 08, 2016 am 09:27 AM
echo int strftime string strtotime

1, get local timestamp
PHP uses the mktime() function to convert a time into a UNIX timestamp value. The timestamp is a long integer, including the UNIX era (January 1, 1070). Many times are based on this as the starting point. Interested friends can search for it. There must be stories in it.
The syntax format of the mktime() function is as follows:
int mktime(int time,int minute,int second,int month,int day,int year,int [is_dis])
The parameters of the mktime() function are explained in the following table:

2. Get the time and date

The current UNIX timestamp through the time() function in php. The syntax format is as follows:
int time(void)
The date() function in PHP is used to get the current time and date. The date() function format is as follows:
date(string format,int timestamp)
The date format functions are as shown in the following table:

getdate() function is mainly used to obtain relevant information in dates. The format of getdate() is as follows:
array getdate(int timestamp)
The returned array elements are as shown in the following table:

The specific sample code is as follows:

<code><span><span><?php</span><span>$array</span> = getdate();
<span>echo</span><span>$array</span>[<span>'year'</span>].<span>"-"</span>.<span>$array</span>[<span>'mon'</span>].<span>"-"</span>.<span>$array</span>[<span>'mday'</span>];
<span>?></span></span></code>
Copy after login

The running results are as follows:

3. Check the validity of the date
Checking the validity of the date is mainly implemented through the checkdate() function. The specific syntax is as follows:
bool checkdate(int month,int day,int year)
Let’s use code to implement it below:

<code><span><?php</span><span>$year</span> =<span>2011</span>;
<span>$month</span> =<span>2</span>;
<span>$day</span> =<span>31</span>;
<span>echo</span> var_dump(checkdate(<span>$day</span>,<span>$month</span>,<span>$year</span>));
<span>?></span></code>
Copy after login

The running results are:

4, output the formatted time and date
This is mainly achieved through the date() function.
The format parameters are as follows:

5, first the localized time and date

The localization of time and date is mainly achieved through the setlocale() function.
The specific syntax is as follows:
string setlocale(string category,string locale)
The options for the parameter category are as shown in the following table:

If the parameter locale is empty, the locale or lang value of the system variable will be used, otherwise the localization environment specified by locale will be applied. For example, en_US is the localized environment of the United States, chs refers to Simplified Chinese, and cht refers to Traditional Chinese.

strftime() function
The strftime() function formats the output time and date according to the locale environment
The syntax format is as follows:
string strftime( string format, int timestamp)



The specific example code is as follows:

<code><span><?php</span>setlocale(LC_ALL,<span>"en_US"</span>);
<span>echo</span><span>"美国格式:"</span>.strftime(<span>"Today is %A"</span>).<span>"\n"</span>;
setlocale(LC_ALL,<span>"chs"</span>);
<span>echo</span><span>"中文简体格式:"</span>.strftime(<span>"今天是:%A"</span>).<span>"\n"</span>;
setlocale(LC_ALL,<span>"cht"</span>);
<span>echo</span><span>"繁体中文格式:"</span>.strftime(<span>"今天是 :%A"</span>).<span>"\n"</span>;


<span>?></span></code>
Copy after login

The running results are as follows:

6, parse the time and date into UNIX timestamp
It is mainly implemented through the strtotime() function.
int strtotime(string time,[,int time])
This function has two parameters. If time is an absolute time, the now parameter has no effect. If the format of the time parameter is relative time, then the corresponding time is provided by now. If now time is not provided, the corresponding time is the current time. Returns false if parsing fails.
The sample code is as follows:

<code><span><span><?php</span><span>echo</span> strtotime(<span>"now"</span>).<span>"\n"</span>;    <span>//当前时间的时间戳</span><span>echo</span><span>"输出时间:"</span>.date(<span>"Y-m-d H:i:s"</span>,strtotime(<span>"now"</span>)).<span>"\n"</span>;  <span>//按照格式要求输出当前的时间</span><span>echo</span> strtotime(<span>"21 May 2009"</span>).<span>"\n"</span>;     <span>//输出指定时间的时间戳</span><span>echo</span><span>"输出时间:"</span>.date(<span>"Y-m-d H:i:s"</span>,strtotime(<span>"21 May 2009"</span>)).<span>"\n"</span>;  <span>//按照格式输出指定时间的时间</span><span>echo</span> strtotime(<span>"+3 day"</span>).<span>"\n"</span>;  <span>// 输出三天以后的时间戳</span><span>echo</span><span>"输出时间:"</span>.date(<span>"Y-m-d H:i:s"</span>,strtotime(<span>"+3 day"</span>)).<span>"\n"</span>;  <span>//按照格式输出三天后的时间</span><span>echo</span> strtotime(<span>"+1 week"</span>).<span>"\n"</span>;    <span>//输出一周以后的时间戳</span><span>echo</span><span>"输出时间:"</span>.date(<span>"Y-m-d H:i:s"</span>,strtotime(<span>"+1 week"</span>)).<span>"\n"</span>;   <span>//按照格式输出一周以后的时间</span><span>echo</span> strtotime(<span>"+1 week 2 days 3 hours 4 minutes"</span>).<span>"\n"</span>;
<span>echo</span> strtotime(<span>"next week"</span>).<span>"\n"</span>;
<span>echo</span> strtotime(<span>"last Monday"</span>).<span>"\n"</span>;


<span>?></span></span></code>
Copy after login

The running results are as follows:

The above introduces the time of PHP development, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

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)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Detailed explanation of the method of converting int type to bytes in PHP Detailed explanation of the method of converting int type to bytes in PHP Mar 06, 2024 pm 06:18 PM

Detailed explanation of the method of converting int type to byte in PHP In PHP, we often need to convert the integer type (int) to the byte (Byte) type, such as when dealing with network data transmission, file processing, or encryption algorithms. This article will introduce in detail how to convert the int type to the byte type and provide specific code examples. 1. The relationship between int type and byte In the computer field, the basic data type int represents an integer, while byte (Byte) is a computer storage unit, usually 8-bit binary data

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

C++ program to convert double type variable to int type C++ program to convert double type variable to int type Aug 25, 2023 pm 08:25 PM

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values ​​available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

What is the value range of int32? What is the value range of int32? Aug 11, 2023 pm 02:53 PM

The value range of int32 is from -2 to the 31st power to 2 to the 31st power minus 1, that is, -2147483648 to 2147483647. int32 is a signed integer type, which means it can represent positive numbers, negative numbers, and zero. It uses 1 bit to represent the sign bit, and the remaining 31 bits are used to represent the numerical value. Since one bit is used to represent the sign bit, the effective number of int32 bits is 31.

How to convert int to string type in go language How to convert int to string type in go language Jun 04, 2021 pm 03:56 PM

Conversion method: 1. Use the Itoa() function, the syntax "strconv.Itoa(num)"; 2. Use the FormatInt() function to convert int type data into the specified base and return it in the form of a string, the syntax "strconv .FormatInt(num,10)".

See all articles