What is the use of PHP's time() function?

青灯夜游
Release: 2023-04-04 14:44:01
Original
4534 people have browsed it

The time() function is a built-in function in PHP. It is used to use the current time as a Unix timestamp and return a numeric string representing the time. The following article will give you a detailed introduction to the time() function. I hope it will be helpful to you.

What is the use of PHP's time() function?

time() function

The time() function can return the number of seconds since the Unix epoch The current time of measurement (number of seconds since January 1, 1970 00:00:00 GMT).

Let’s take a look at a simple example of using the time() function to output the current time in seconds:

<?php 
date_default_timezone_set(&#39;PRC&#39;);
$timestamp = time();  
echo $timestamp; 
?>
Copy after login

Running rendering:

What is the use of PHPs time() function?

Note: Use PHP's date_default_timezone_set() function to set the time zone, so that the current time obtained by the time() function is more accurate.

Format output time

The time obtained by the time() function is in seconds, which is not convenient for us to view Time, we can also use the date() function in PHP to convert seconds into a time format that is convenient for viewing.

Example: Assume that the current time is after 1 pm

<?php 
date_default_timezone_set(&#39;PRC&#39;);
$timestamp = time();  
echo $timestamp; 

echo "<br><br>"; 
echo(date("Y-m-d h:i:s A", $timestamp)); 
echo "<br><br>"; 
echo(date("Y年m月d日, H:i:s", $timestamp)); 
echo "<br><br>"; 
echo(date("Y , F d , H:i:s ", $timestamp)); 
?>
Copy after login

Rendering

What is the use of PHP's time() function?

The date() function can use certain Characters to format the time and output a specific time. The following are some commonly used date() function formatting characters:

Character Y: Output the 4-digit number representing the year

Character m: Output A 2-digit number representing the month, starting from 01 and ending at 12

Character F: Output the full English name of the month, for example: December

Character d: Output 2 representing a certain day of the current month Digits, starting from 01 and ending at 30 (or 31)

Character h: Output a 2-digit number representing the hour (12-hour format) starting from 01 and ending at 12. Note: You need to use the character a (lowercase output) or character A (uppercase output) to represent morning (am) or afternoon (pm).

Character H: output a 2-digit number representing the hour (24-hour format), starting from 00 and ending at 23

Character i: outputting a 2-digit number representing the minute, starting from 00 to Ending at 59

Character s: Output a 2-digit number representing seconds, starting from 00 and ending at 59

Summary: The above is the entire content of this article, I hope It can be helpful to everyone’s study.

The above is the detailed content of What is the use of PHP's time() function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!