如何从日期找到一个月的最后一天?

新手上路,请多包涵

如何在 PHP 中获取一个月的最后一天?

鉴于:

 $a_date = "2009-11-23"

我要2009-11-30;并给出

$a_date = "2009-12-23"

我想要 2009 年 12 月 31 日。

原文由 Mithun Sreedharan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 371
2 个回答

t 返回给定日期当月的天数(参见 date 的文档):

 $a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

原文由 Dominic Rodger 发布,翻译遵循 CC BY-SA 2.5 许可协议

2行代码,你就完成了:

 $oDate = new DateTime("2019-11-23");

// now your date object has been updated with last day of month
$oDate->setDate($oDate->format("Y"),$oDate->format("m"),$oDate->format("t"));

// or to just echo you can skip the above line using this
echo $oDate->format("Y-m-t");

原文由 justnajm 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题