Home  >  Article  >  Backend Development  >  How to convert timestamp to date in js and php

How to convert timestamp to date in js and php

PHPz
PHPzOriginal
2023-03-21 17:16:361316browse

Both JavaScript and PHP support timestamp processing. JavaScript uses Date objects to handle dates and times, while PHP uses the date() function to format timestamps. This article will introduce you to how to convert between timestamps and dates in JavaScript and PHP.

JavaScript timestamp conversion date

In JavaScript, we can use new Date() to create a date object. You can use the getTime() method to obtain a timestamp, which returns the number of milliseconds from January 1, 1970, 0:00:00 (UTC) to the time of the instance object. Below is a sample code that converts a timestamp to a local date.

var timestamp = 1521315123;
var date = new Date(timestamp*1000);//timestamp必须乘以1000,因为getTime()返回的是毫秒数
console.log(date.toLocaleString());//输出本地日期和时间

In this example, the time obtained from the Unix timestamp is 1521315123. Note that getTime() returns milliseconds, so the timestamp must be multiplied by 1000.

You can also use the toLocaleString() method to format the date and convert it to a local date. The toLocaleString() method returns a string similar to "March 18, 2018 1:25:23 PM".

PHP Timestamp Conversion Date

In PHP, we can use the date() function to format a timestamp into a date string. Below is a sample code that converts a timestamp into the format of "Year-Month-Day Hour:Minute:Second".

$timestamp = 1521315123;
$date = date('Y-m-d H:i:s',$timestamp);
echo $date;//输出2018-03-18 13:25:23

In this example, we use the date() function to format the timestamp. The first parameter is a formatted format string, where Y, m, d, H, i, and s are format codes representing the year, month, day, and 24-hour hours, minutes, and seconds. The second parameter is a timestamp. The date() function converts the timestamp into a date and time string in the specified format.

If you want to format in a certain time zone, you can set the date_default_timezone_set() function. For example, to format a timestamp in the New York time zone, execute the following code:

date_default_timezone_set('America/New_York');
$timestamp = 1521315123;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;//输出2018-03-18 09:25:23 (因为与本地时区的差距是4小时)

In this example, the date_default_timezone_set() function sets the time zone to "America/New_York", which is New York time. We then use the date() function to format the timestamp into a string.

Conclusion

In the above example, we introduced how to convert timestamp to datetime format in JavaScript and PHP and tested it. Regardless of the language, these methods are very simple and easy to understand. Whether you develop in JavaScript or PHP, you can use these methods to handle conversion between timestamps and dates.

The above is the detailed content of How to convert timestamp to date in js and 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