Fir*_*rze 6 php datetime date unix-timestamp
我有2个日期是unix时间戳,我必须弄清楚日期是否在同一个月.例如,如果我有1393624800 = 02 / 28 / 14和1391472000 = 2 / 4 / 2014.我想我可以将unix时间戳转换为格式28-02-2014并提取月份,但我觉得它有点脏.做这个的最好方式是什么?
编辑:我忘了.还必须确保它在同一年.
Mik*_*ant 16
我建议使用DateTime类
$date1 = new DateTime();
$date1->setTimestamp($timestamp1);
$date2 = new DateTime();
$date2->setTimestamp($timestamp2);
if ($date1->format('Y-m') === $date2->format('Y-m')) {
// year and month match
}
Run Code Online (Sandbox Code Playgroud)
我强烈建议任何学习PHP的人学习DateTime和相关课程.它们比基本日期功能强大得多.