在PHP中,可以使用date()函數(shù)將時(shí)間戳轉(zhuǎn)換為指定格式的日期時(shí)間字符串。以下是一些常用的技巧和示例:
$timestamp = 1620785847;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
$dateString = '2021-05-12 15:30:00';
$timestamp = strtotime($dateString);
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
$timestamp = 1620785847;
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d H:i:s');
$dateString = '2021-05-12 15:30:00';
$date = new DateTime($dateString);
echo $date->getTimestamp();
這些是一些常用的技巧,可以幫助您在PHP中處理時(shí)間戳和日期時(shí)間字符串的轉(zhuǎn)換。您可以根據(jù)具體的需求和場(chǎng)景選擇適合的方法進(jìn)行處理。