其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

使用PHP如何把中文格式“2022年03月14日 08时00分00秒”转换成时间戳

GG网络技术分享 2025-03-18 16:17 0


可以使用strtotime()函数将日期时间字符串转换为时间戳。但是,strtotime()函数只能识别英文日期格式,无法识别中文日期格式。因此,需要先将中文日期格式转换为英文日期格式,然后再使用strtotime()函数将其转换为时间戳。

以下是将中文日期格式转换为英文日期格式的示例代码:

$date_str = \'2022年03月14日 08时00分00秒\';

$date_str = str_replace([\'年\', \'月\', \'日\', \'时\', \'分\', \'秒\'], [\'-\', \'-\', \'\', \':\', \':\', \'\'], $date_str);

$timestamp = strtotime($date_str);

echo $timestamp;

在上面的代码中,我们使用str_replace()函数将中文日期格式中的年、月、日、时、分、秒替换为英文日期格式中的分隔符,然后再使用strtotime()函数将其转换为时间戳。

注意,strtotime()函数返回的时间戳是基于当前时区的。如果需要使用其他时区,可以使用date_default_timezone_set()函数设置时区。

标签: 日期 格式

提交需求或反馈

Demand feedback