Introduction to PHP functions: time() function

PHPz
Release: 2023-11-03 08:46:01
Original
2415 people have browsed it

Introduction to PHP functions: time() function

PHP function introduction: time() function

PHP is a commonly used server-side programming language with powerful function library support. Among them, the time() function is one of the commonly used time functions in PHP. This article will introduce in detail the role, usage and specific code examples of the time() function.

1. The role of time() function

The time() function is a built-in function in PHP, used to obtain the current Unix timestamp (that is, from January 1, 1970 00: 00:00 UTC to the current time in seconds).

2. How to use the time() function

The time() function has no parameters to pass and can be called directly. It returns an integer value representing the current Unix timestamp.

The following is a basic usage example:

<?php
    $timestamp = time();
    echo "当前时间戳:".$timestamp;
?>
Copy after login
Copy after login

The above code will output the current Unix timestamp.

3. Code example of time() function

  1. Output the current timestamp
<?php
    $timestamp = time();
    echo "当前时间戳:".$timestamp;
?>
Copy after login
Copy after login
  1. Judge whether a specific date has passed
<?php
    $targetDate = strtotime("2023-02-15");
    $currentTime = time();
    
    if($currentTime > $targetDate) {
        echo "指定日期已过去";
    } else {
        echo "指定日期尚未到来";
    }
?>
Copy after login

In the above code, we use the strtotime() function to convert the specified date into a Unix timestamp and compare it with the current timestamp to determine whether the specified date has passed.

  1. Get the current time
<?php
    date_default_timezone_set('Asia/Shanghai');
    
    $currentTime = time();
    $currentDate = date("Y-m-d", $currentTime);
    $currentTime = date("H:i:s", $currentTime);
    
    echo "当前日期:".$currentDate."<br>";
    echo "当前时间:".$currentTime;
?>
Copy after login

In this example, we use the date_default_timezone_set() function to set the time zone to Shanghai time zone, and then use the date() function to convert the current timestamp Format the specified date and time and output.

4. Summary

The time() function is one of the commonly used time functions in PHP. It can easily obtain the current Unix timestamp. By converting timestamps, we can perform various time-related operations and judgments. At the same time, we can also use the date() function to convert the timestamp into different date and time formats. I hope the introduction in this article can help you better understand and use the time() function.

The above is the detailed content of Introduction to PHP functions: time() function. For more information, please follow other related articles on the PHP Chinese website!

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