温馨提示×

php strtodate怎样转换日期格式

PHP
小樊
81
2024-10-17 07:14:38
栏目: 编程语言

在PHP中,可以使用strtotime()函数将字符串转换为日期。strtotime()函数接受两个参数:要转换的字符串和可选的日期字符串。如果提供了日期字符串,strtotime()会将其视为基准日期来解析输入的字符串。

以下是一个示例,说明如何使用strtotime()函数将字符串转换为日期:

<?php
// 示例字符串
$dateString = "2022-01-01 12:00:00";

// 使用 strtotime() 函数将字符串转换为日期
$date = strtotime($dateString);

// 使用 date() 函数将 Unix 时间戳转换为可读的日期格式
$formattedDate = date('Y-m-d H:i:s', $date);

// 输出转换后的日期
echo "Converted date: " . $formattedDate;
?>

在这个示例中,我们首先定义了一个包含日期的字符串$dateString。然后,我们使用strtotime()函数将其转换为Unix时间戳。最后,我们使用date()函数将Unix时间戳转换为可读的日期格式,并将其输出。

注意:strtotime()函数可以解析许多不同的日期字符串格式。你可以在PHP文档中找到所有支持的格式:https://www.php.net/manual/en/function.strtotime.php

0