PHP learning about time_PHP tutorial
Functions about time:
strftime Format local time/date according to locale
strptime — parse date/time generated by strftime()
setlocale function sets regional information (regional information)
date_default_timezone_set If you find that the number of hours obtained by the date function is 8 hours different from the actual number, please add date_default_timezone_set('Etc/GMT-8');
date — Format a local time/date
gmdate is exactly the same as date — formats a GMT/UTC date/time
mktime — Get the Unix timestamp of a date mktime(0, 0, 0, 12, 30, 1997)
gmmktime and mktime are exactly the same
strtotime — Parse any English text datetime description into a Unix timestamp
time — Returns the current Unix timestamp
microtime — Returns the current Unix timestamp and microseconds
getlastmod — Get the time when the web content was last updated on the currently used web page
Note: TimeStamp is not a function, it refers to the Unix timestamp, which is the number of seconds from 0:00:00 on January 1, 1970 to this moment
Practical operation cases
$strtime = "2000-02-12 16:20:35";
$array = explode("-",$strtime);
$year = $array[0];
$month = $array[1];
var_dump($array);
$array = explode(":",$array[2]);
$minute = $array[1];
$second = $array[2];
var_dump($array);
$array = explode(" ",$array[0]);
$day = $array[0];
$hour = $array[1];
$timestamp = mktime($hour,$minute,$second,$month,$day,$year);
echo "String time: $strtime
";
echo "Year: $year
";
echo "Month: $month
";
echo "Day: $day
";
echo "hour: $hour
";
echo "minute: $minute
";
echo "Seconds: $second
";
echo "Convert to timestamp:" . $timestamp . "
";
echo "Convert back from timestamp:" . date("y-m-d h:i:s",$timestamp) . "
";
echo "Convert back from timestamp:" . date("y-m-d h:i:s","1288263141") . "
";
header("Content-Type:text/html; charset=utf-8");
echo (strtotime("2010-10-28 10:52:21")). "
"; // Convert the string in MySQL format into seconds
echo (strtotime("2010-10-01 00:00:00")). "
";
$d = date("Y-m-d H:i:s", time()); // Convert seconds to timestamp
in MySQL format
echo $d. "
";
The result is:
array
0 => string '2000' (length=4)
1 => string '02' (length=2)
2 => string '12 16:20:35' (length=11)
array
0 => string '12 16' (length=5)
1 => string '20' (length=2)
2 => string '35' (length=2)
String time: 2000-02-12 16:20:35
Year: 2000
Month: 02
Day: 12
Time: 16
Points: 20
Seconds: 35
Convert to timestamp: 950372435
Convert back from timestamp: 00-02-12 04:20:35
Convert back from timestamp: 10-10-28 10:52:21
1288263141
1285891200
2011-10-20 14:48:27
Common values for formatting time functions
The function date (string format [, int timestamp]) can format date/time. The parameter format is the format string. The most commonly used values are as follows:
Y: year represented by 4 digits
y: year represented by 2 digits
m: month represented by number
M: The month represented by the three-letter abbreviation
d: Day of the month
D: What day of the week
h: Hour, 12-hour format, with leading zeros
H: Hour, 24-hour format, with leading zeros
i: Minutes with leading zeros
I: Whether it is daylight saving time
s: seconds
S: English suffix after the day of the month, 2 characters
w: Day of the week, represented by numbers
W: Week number of the year in ISO-8601 format, each week starts on Monday
l: Day of the week, complete text format
L: Whether it is a leap year
g: 12 hour format, no leading zeros
G: 24 hour format, no leading zeros
If you find that the number of hours obtained by the date function is 8 hours different from the actual number, please add date_default_timezone_set('Etc/GMT-8');
The getdate(timestamp) function can obtain date/time information.
Returns an associative array containing date information based on timestamp. If no timestamp is given, the current local time is assumed.
The cells in the array are as follows:
Key name
Description
Return value example
"seconds"
Numerical representation of seconds
0 to 59
"minutes"
Numerical representation of minutes
0 to 59
"hours"
Numerical representation of hours
0 to 23
"mday"
The numerical representation of the day of the month
1 to 31
"wday"
The numerical representation of the day of the week
0 (meaning Sunday) to 6 (meaning Saturday)
"mon"
Number representation of month
1 to 12
"year"
Complete year represented by 4 digits
For example: 1999 or 2003
"yday"
The numerical representation of the day of the year
0 to 365
"weekday"
Full textual representation of the day of the week
Sunday to Saturday
"month"
Full text representation of month
January to December
0
The number of seconds since the Unix epoch, similar to the return value of time() and the value used for date().
System dependent, typical values are from -2147483648 to 2147483647.
Example:
//Display format: Year-Month-Day Hour:Minute:Miao
$showtime=date("Y-m-d H:i:s");
echo 'The format of display time is:'.$showtime."
";
The result is:
The format for displaying time is: 2011-10-20 14:48:27
This article comes from the “php technology” blog

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

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
