从以strtotime格式保存的日期中仅提取年份和月份

时间:2016-11-24 06:09:00

标签: php date strtotime

我的日期字符串值为1478025000。想要从字符串中获取字符串格式的年份和月份,是否可能?那天应该是0,下个月和明年就是这样。 我想要的输出应该是1477852200

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

$epoch = 1478025000;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY

如果您想将年份和月份作为一个时代,请尝试以下方法:

$epoch = 1478025000;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();