如何在PHP中获得每月的最后一天?
考虑到:
$a_date = "2009-11-23"
我想要2009-11-30;鉴于
$a_date = "2009-12-23"
我要2009-12-31。
如何在PHP中获得每月的最后一天?
考虑到:
$a_date = "2009-11-23"
我想要2009-11-30;鉴于
$a_date = "2009-12-23"
我要2009-12-31。
当前回答
你的解决方案在这里。
$lastday = date('t',strtotime('today'));
其他回答
有很多方法可以得到一个月的最后一天。
//to get last day of current month
echo date("t", strtotime('now'));
//to get last day from specific date
$date = "2014-07-24";
echo date("t", strtotime($date));
//to get last day from specific date by calendar
$date = "2014-07-24";
$dateArr=explode('-',$date);
echo cal_days_in_month(CAL_GREGORIAN, $dateArr[1], $dateArr[0]);
T返回给定日期当月的天数(date参见文档):
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
碳API扩展PHP DateTime
Carbon::parse("2009-11-23")->lastOfMonth()->day;
or
Carbon::createFromDate(2009, 11, 23)->lastOfMonth()->day;
将返回
30
function first_last_day($string, $first_last, $format) {
$result = strtotime($string);
$year = date('Y',$result);
$month = date('m',$result);
$result = strtotime("{$year}-{$month}-01");
if ($first_last == 'last'){$result = strtotime('-1 second', strtotime('+1 month', $result)); }
if ($format == 'unix'){return $result; }
if ($format == 'standard'){return date('Y-m-d', $result); }
}
http://zkinformer.com/?p=134
我知道这有点晚了,但我认为有一种更优雅的方式来使用PHP 5.3+使用DateTime类:
$date = new DateTime('now');
$date->modify('last day of this month');
echo $date->format('Y-m-d');