Formatting Unix Timestamps as Date Strings in PHP
Converting Unix timestamps to formatted date strings is a common task in PHP-based applications. By formatting these numerical representations, you can display timestamps in a human-readable format, such as "2008-07-17T09:24:17Z".
For example, you want to convert a timestamp like 1333699439 to the string "2008-07-17T09:24:17Z". To achieve this, you can use the gmdate() function, which formats a timestamp according to the specified format string. Here's how you can do it:
<?php $timestamp = 1333699439; $formattedDate = gmdate("Y-m-d\TH:i:s\Z", $timestamp); echo $formattedDate; ?>
In this code, the gmdate() function is used with the following format string:
By composing these format specifiers, you can create a custom date string format that matches your desired output. The gmdate() function will then apply this format to the provided timestamp, resulting in a formatted date string in UTC time.
The above is the detailed content of How to Format Unix Timestamps as Date Strings in PHP?. For more information, please follow other related articles on the PHP Chinese website!