PHP 转换日期时间字符串为标准格式 - 25/11/2020下午4:00:00 示例
可以使用 strtotime() 函数将字符串转换为时间戳,然后使用 date() 函数将时间戳格式化为标准时间格式。
示例代码如下:
$time_str = '25/11/2020下午4:00:00';
$time_stamp = strtotime(str_replace('下午', 'pm', $time_str));
$time_format = date('Y-m-d H:i:s', $time_stamp);
echo $time_format;
输出结果为:2020-11-25 16:00:00
解释说明:
- 使用
str_replace()函数将字符串中的 '下午' 替换为 'pm',以便strtotime()函数能够正确解析。 - 使用
strtotime()函数将字符串转换为时间戳。 - 使用
date()函数将时间戳格式化为标准时间格式。 - 输出格式化后的时间字符串。
原文地址: https://www.cveoy.top/t/topic/lBmV 著作权归作者所有。请勿转载和采集!