如何在PHP中获取本月的最后一天?
鉴于:
$a_date = "2009-11-23"
Run Code Online (Sandbox Code Playgroud)
我想要2009-11-30; 给定
$a_date = "2009-12-23"
Run Code Online (Sandbox Code Playgroud)
我想2009-12-31.
Dom*_*ger 762
t返回给定日期的月份天数(请参阅文档date):
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
Run Code Online (Sandbox Code Playgroud)
Mug*_*nth 108
使用strtotime()的代码将在2038年后失败.(如此线程中的第一个答案所示)例如,尝试使用以下代码:
$a_date = "2040-11-23";
echo date("Y-m-t", strtotime($a_date));
Run Code Online (Sandbox Code Playgroud)
它将给出答案:1970-01-31
因此,应该使用DateTime函数而不是strtotime.以下代码将在没有2038年问题的情况下运行:
$d = new DateTime( '2040-11-23' );
echo $d->format( 'Y-m-t' );
Run Code Online (Sandbox Code Playgroud)
Han*_*sir 97
我知道这有点晚了但我认为PHP 5.3+通过使用DateTime类有更优雅的方法:
$date = new DateTime('now');
$date->modify('last day of this month');
echo $date->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)
MPV*_*MPV 69
还有内置的PHP函数cal_days_in_month()?
"此功能将返回指定日历的一年中的天数." http://php.net/manual/en/function.cal-days-in-month.
echo cal_days_in_month(CAL_GREGORIAN, 11, 2009);
// = 30
Run Code Online (Sandbox Code Playgroud)
kal*_*azy 24
这应该工作:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
Run Code Online (Sandbox Code Playgroud)
Moh*_*eer 14
试试这个,如果您使用的是PHP 5.3+,
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)
要查找下个月的最后日期,请修改如下,
$date->modify('last day of 1 month');
echo $date->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)
等等..
joh*_*ith 13
有什么不对 - 对我来说最优雅的是使用 DateTime
我不知道我没有看到DateTime::createFromFormat,单行
$lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t");
Run Code Online (Sandbox Code Playgroud)
如今,如果您有月份和年份,则 DateTime 可以非常方便地执行此操作
$date = new DateTime('last day of '.$year.'-'.$month);
Run Code Online (Sandbox Code Playgroud)
从另一个 DateTime 对象将是
$date = new DateTime('last day of '.$otherdate->format('Y-m'));
Run Code Online (Sandbox Code Playgroud)
如果您对PHP DateTime 使用Carbon API扩展,则可以使用以下内容获取该月的最后一天:
$date = Carbon::now();
$date->addMonth();
$date->day = 0;
echo $date->toDateString(); // use toDateTimeString() to get date and time
Run Code Online (Sandbox Code Playgroud)
您可以通过几种方式找到每月的最后一天。但是简单地可以使用PHP strtotime()和date()函数来完成此操作,我想您的最终代码将如下所示:
$a_date = "2009-11-23";
echo date('Y-m-t',strtotime($a_date));
Run Code Online (Sandbox Code Playgroud)
但是,如果您使用的是PHP> = 5.2,我强烈建议您使用新的DateTime对象。例如下面的例子:
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)
另外,您可以使用自己的函数来解决此问题,如下所示:
/**
* Last date of a month of a year
*
* @param[in] $date - Integer. Default = Current Month
*
* @return Last date of the month and year in yyyy-mm-dd format
*/
function last_day_of_the_month($date = '')
{
$month = date('m', strtotime($date));
$year = date('Y', strtotime($date));
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
$a_date = "2009-11-23";
echo last_day_of_the_month($a_date);
Run Code Online (Sandbox Code Playgroud)
PHP DateTime 的Carbon API 扩展
Carbon::parse("2009-11-23")->lastOfMonth()->day;
Run Code Online (Sandbox Code Playgroud)
或者
Carbon::createFromDate(2009, 11, 23)->lastOfMonth()->day;
Run Code Online (Sandbox Code Playgroud)
将返回
30
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
419644 次 |
| 最近记录: |