从以 strtotime 格式保存的日期中仅获取年份和月份
fetching only year and month from a date saved in the format of strtotime
我的日期字符串值为 1478025000
。想从这个字符串中获取字符串格式本身的年份和月份,这可能吗?这一天应该是 0,下个月和明年也是这样。
我想要的输出应该是 1477852200
尝试这样的事情:
$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();
我的日期字符串值为 1478025000
。想从这个字符串中获取字符串格式本身的年份和月份,这可能吗?这一天应该是 0,下个月和明年也是这样。
我想要的输出应该是 1477852200
尝试这样的事情:
$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();