如何从日期获取年份和月份 - PHP

2022-08-30 11:46:19

如何从给定日期获取年份和月份。

例如:$dateValue = '2012-01-05';

从这个日期开始,我需要将年份设置为2012年,将月份设置为1月


答案 1

使用 strtotime()

$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);

答案 2

从文档中使用 date()strtotime()。

$date = "2012-01-05";

$year = date('Y', strtotime($date));

$month = date('F', strtotime($date));

echo $month

推荐