Home >Backend Development >PHP Problem >How to convert 13-digit timestamp into specific time format in PHP
When dealing with time-related business, we often involve timestamp conversion. In PHP, timestamps are usually expressed as 10-digit integers or 13-digit integers, where 10-digit integers represent the number of seconds from January 1, 1970 to the current time, while 13-digit integers represent the number of seconds from January 1, 1970 to The current time in milliseconds. However, sometimes we need to convert the 13-digit timestamp into a specific date and time, which requires the use of some related PHP functions.
1. PHP method of obtaining 13-digit timestamp
We can use the PHP function time() to obtain the current 10-digit timestamp, and for 13-digit timestamps, you need to use the microtime() function. The microtime() function returns the number of microseconds and seconds of the current time. We can obtain the 13-digit timestamp through formatted output. Refer to the following code:
<?php // 获取当前时间13位时间戳 list($msec, $sec) = explode(' ', microtime()); $timestamp = sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); echo $timestamp; ?>
In this code, the list() function Used to obtain the two values in the array returned by the microtime() function, namely the number of microseconds and the number of seconds. We then add them together and multiply by 1000 to get a 13-digit timestamp. Finally, the timestamp is output through the echo statement.
2. PHP converts the 13-digit timestamp into date and time
After getting the 13-digit timestamp, we can use the date() function to convert it into a specific date and time. The date() function can accept two parameters, the first parameter represents the format string, and the second parameter represents the timestamp to be formatted. Refer to the following code:
<?php // 将13位时间戳转换成日期和时间 $timestamp = 1607914806536; // 假设要转换的13位时间戳 $datetime = date('Y-m-d H:i:s', $timestamp/1000); echo $datetime; ?>
Here, we assume that the 13-digit timestamp to be converted is 1607914806536 milliseconds. We first need to divide it by 1000 to convert it into a 10-digit timestamp, and then call the date() function to format it into the form Y-m-d H:i:s. Finally, the date and time are output through the echo statement.
3. PHP converts date and time into a 13-digit timestamp
Another common requirement is to convert a specific date and time into a 13-digit timestamp. PHP provides the strtotime() function to achieve this requirement. The strtotime() function can accept a date or datetime string and convert it to a Unix timestamp. Refer to the following code:
<?php // 将日期和时间转换成13位时间戳 $datetime = '2020-12-14 18:33:26'; // 假设要转换的日期和时间 $timestamp = strtotime($datetime) * 1000; echo $timestamp; ?>
Here, we assume that the date and time to be converted is 2020-12-14 18:33:26. We first call the strtotime() function to convert it to a Unix timestamp, then multiply it by 1000 to get a 13-digit timestamp. Finally, the 13-digit timestamp is output through the echo statement.
To sum up, we can see that PHP provides a series of functions for converting between timestamps and specific dates and times. In actual development, we should choose appropriate functions to implement timestamp related operations based on business needs.
The above is the detailed content of How to convert 13-digit timestamp into specific time format in PHP. For more information, please follow other related articles on the PHP Chinese website!