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

How to convert unix timestamp in php

PHPz
PHPzOriginal
2023-03-24 14:25:531403browse

When developing websites and applications, we often need to deal with time and date. In PHP, timestamp is a very common time format, which represents the number of seconds that have elapsed since January 1, 1970, 0:00:00 (UTC). However, the time format displayed by Unix timestamp is not human-friendly, so in practical applications we need to convert it into a more readable date and time format. This article will explain how to convert Unix timestamp to common date and time formats in PHP.

  1. Convert Unix timestamp to date

In PHP, we can use the date function to convert Unix timestamp to date. The date function accepts two parameters: one is the date format and the other is the Unix timestamp. Here is an example:

$timestamp = time(); //获取当前时间的时间戳
$date = date('Y-m-d', $timestamp); //将时间戳转换为日期格式
echo $date; //输出2021-06-14

In the above example, we used the time function to get the Unix timestamp of the current time, and then used the date function to convert it to the year-month-day date format. If you want to convert the timestamp to other formats, you can check PHP's official documentation and modify it as needed.

  1. Convert Unix timestamp to time

Similar to date, we can also use the date function to convert Unix timestamp to time format. The following is an example:

$timestamp = time(); //获取当前时间的时间戳
$time = date('H:i:s', $timestamp); //将时间戳转换为时间格式
echo $time; //输出21:29:50

In the above example, we also use the time function to obtain the Unix timestamp of the current time, and then use the date function to convert it into the hour:minute:second time format. If you want to convert the timestamp to other formats, you can also check PHP's official documentation and modify it as needed.

  1. Convert Unix timestamp to date time

If we need to convert Unix timestamp to a more precise date time format, we can Date and time formats are used in combination. The following is an example:

$timestamp = time(); //获取当前时间的时间戳
$date_time = date('Y-m-d H:i:s', $timestamp); //将时间戳转换为日期时间格式
echo $date_time; //输出2021-06-14 21:35:40

In the above example, we combine the date and time formats and use the date function to convert the Unix timestamp to year-month-day hour:minute:second. Date time format. Similarly, if you want to convert the timestamp to another format, you can check PHP's official documentation and modify it as needed.

  1. Convert date time to Unix timestamp

In addition to converting Unix timestamp to date time format, we sometimes also need to convert date time Format converted to Unix timestamp. In PHP, we can convert datetime format to Unix timestamp using strtotime function. Here is an example:

$date_time = '2021-06-14 21:35:40'; //待转换的日期时间
$timestamp = strtotime($date_time); //将日期时间转换为Unix时间戳
echo $timestamp; //输出1623692140

In the above example, we define the datetime format as a string and then use the strtotime function to convert it to a Unix timestamp. Similarly, if you want to convert other date and time formats into Unix timestamps, you can check PHP's official documentation and modify it as needed.

Summary

This article introduces how to convert Unix timestamp to common date and time formats in PHP, and how to convert date time format to Unix timestamp . Mastering this knowledge allows us to handle time and dates more conveniently, making development work more efficient. Of course, there are many other techniques and knowledge for processing time and date, and I hope readers can study them in depth and master them.

The above is the detailed content of How to convert unix 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