Home  >  Article  >  Backend Development  >  How to convert timestamp in php

How to convert timestamp in php

PHPz
PHPzOriginal
2023-03-23 11:10:383268browse

Time stamp refers to the number of seconds from 0:00:00 on January 1, 1970 to the present. It is widely used in the computer field. In PHP, the use of timestamps is also very common, so learning how to convert timestamps is also one of the essential skills for PHP beginners.

1. Convert timestamp to time string

To convert timestamp to time string, you can use the date() function. The first parameter of the date() function is the format string, followed by the timestamp. The following is an example:

    $timestamp = time(); //获取当前时间戳
    $timeStr = date("Y-m-d H:i:s", $timestamp); //将时间戳转换为时间字符串,格式为“年-月-日 时:分:秒”
    echo $timeStr; //输出“2021-11-11 11:11:11”

In the above code, "Y" represents the year, "m" represents the month, "d" represents the date, "H" represents the hour, "i" represents the minute, "s" Represents seconds.

2. Convert the time string into a timestamp

To convert the time string into a timestamp, you can use the strtotime() function. The following is an example:

    $timeStr = "2021-11-11 11:11:11";
    $timestamp = strtotime($timeStr); //将时间字符串转换为时间戳
    echo $timestamp; //输出“1636621871”

3. Time zone issue

In PHP, time zone plays a very important role. If your server is located in the East Eighth District and your customer is located in the West Fifth District, the time the customer gets will definitely be 13 hours slower than yours.

However, PHP provides corresponding functions to solve time zone problems. We can use the date_default_timezone_set() function to change the default time zone. The following is an example:

    date_default_timezone_set('Asia/Shanghai'); //设置时区为东八区
    $timestamp = time(); //获取当前时间戳
    $timeStr = date("Y-m-d H:i:s", $timestamp); //将时间戳转换为时间字符串,格式为“年-月-日 时:分:秒”
    echo $timeStr; //输出“2021-11-11 11:11:11”

In the above code, the time zone is set to "Asia/Shanghai", which is the East Eighth District.

4. Timestamp conversion tool class

If you need to frequently convert timestamps and time strings in your project, you can consider encapsulating a timestamp Conversion tool class. The following is a simple timestamp conversion tool class:

class TimestampConverter{
    /**
     * 时间戳转换为时间字符串
     * @param $timestamp 时间戳
     * @param string $format 格式化字符串
     * @return false|string
     */
    public static function timestampToStr($timestamp, $format = "Y-m-d H:i:s") {
        date_default_timezone_set('Asia/Shanghai');
        return date($format, $timestamp);
    }

    /**
     * 时间字符串转换为时间戳
     * @param $timeStr 时间字符串
     * @return false|int
     */
    public static function strToTimestamp($timeStr) {
        date_default_timezone_set('Asia/Shanghai');
        return strtotime($timeStr);
    }
}

In the above code, we put the methods of converting timestamps into time strings and time strings into timestamps in one class for easy management. And maintenance.

Summary

This article introduces how to convert timestamps to time strings, convert time strings to timestamps, and issues about time zones. In addition, we also provide an example of a timestamp conversion tool class, which we hope will be helpful to PHP beginners.

The above is the detailed content of How to convert timestamp in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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