如何使用PHP找到一个月的第一天和最后一天日期?

92

如何使用PHP找到一个月的第一天和最后一天?例如,今天是2010年4月21日;我想要找到2010年4月1日和2010年4月30日。


可能重复:https://dev59.com/MHI-5IYBdhLWcg3wwbdM - Oki Erie Rinaldi
12个回答

231

最简单的方法是使用date函数,它允许您混合硬编码的值和从时间戳中提取的值。如果不提供时间戳,则默认为当前日期和时间。

// Current timestamp is assumed, so these find first and last day of THIS month
$first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
$last_day_this_month  = date('m-t-Y');

// With timestamp, this gets last day of April 2010
$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));

date()搜索给定的字符串(如'm-t-Y'),查找其中特定的符号并将它们替换为其时间戳中的值。因此,我们可以使用这些符号从时间戳中提取我们想要的值和格式。在上面的示例中:

  • Y从时间戳中获取4位数的年份('2010')
  • m从时间戳中获取数字月份,并在前面加上零('04')
  • t从时间戳中获取该月份的天数('30')

您可以根据需要进行创意操作。例如,要获取一个月的第一秒和最后一秒:

$timestamp    = strtotime('February 2012');
$first_second = date('m-01-Y 00:00:00', $timestamp);
$last_second  = date('m-t-Y 12:59:59', $timestamp); // A leap year!
请参见http://php.net/manual/zh/function.date.php获取其他符号和更多详细信息。

3
在此补充一些信息,我在将日期转换回 Unix 时间戳时遇到问题,使用'd-m-Y'格式而不是'm-d-Y'格式进行修复。最后一秒钟应为“23:59:59”,而不是“12:59:59”。 - Qarib Haider

33

简单的一句话

  • Y - 一个完整的、4位数的年份表达
  • m - 数字表示的月份,带前导零
  • t - 给定月份的天数

参考资料 - http://www.php.net/manual/en/function.date.php

<?php
    echo 'First Date    = ' . date('Y-m-01') . '<br />';
    echo 'Last Date     = ' . date('Y-m-t')  . '<br />';
?>

当您将“m”替换为任何月份数字时,它无法正常工作。 - Sumit Kumar Gupta

21
您可以使用date函数找到一个月中有多少天。
// Get the timestamp for the date/month in question.
$ts = strtotime('April 2010');

echo date('t', $ts); 
// Result: 30, therefore, April 30, 2010 is the last day of that month.
希望这有所帮助。 编辑:在阅读路易斯的回答之后,我想到您可能需要将日期格式化为正确的格式(YY-mm-dd)。这可能很明显,但并不妨碍提及。
// After the above code
echo date('Y-m-t', $ts); 

13

这将给你本月的最后一天:

function lastday($month = '', $year = '') {
   if (empty($month)) {
      $month = date('m');
   }
   if (empty($year)) {
      $year = date('Y');
   }
   $result = strtotime("{$year}-{$month}-01");
   $result = strtotime('-1 second', strtotime('+1 month', $result));
   return date('Y-m-d', $result);
}

第一天:

function firstDay($month = '', $year = '')
{
    if (empty($month)) {
      $month = date('m');
   }
   if (empty($year)) {
      $year = date('Y');
   }
   $result = strtotime("{$year}-{$month}-01");
   return date('Y-m-d', $result);
} 

这个函数对我有用。救了我的一天。 - Sumit Kumar Gupta

8

对于特定的月份年份,使用date()函数自然语言输出如下:

$first_date = date('d-m-Y',strtotime('first day of april 2010'));
$last_date = date('d-m-Y',strtotime('last day of april 2010'));
// Isn't it simple way?
但是针对当前月份。
$first_date = date('d-m-Y',strtotime('first day of this month'));
$last_date = date('d-m-Y',strtotime('last day of this month'));

6
如果您想从指定的日期变量中找到第一天和最后一天,可以按以下方式操作:
$date    =    '2012-02-12';//your given date

$first_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", first day of this month");
echo $first_date = date("Y-m-d",$first_date_find);

$last_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", last day of this month");
echo $last_date = date("Y-m-d",$last_date_find);

要获取当前日期,只需简单地使用以下代码:

$first_date = date('Y-m-d',strtotime('first day of this month'));
$last_date = date('Y-m-d',strtotime('last day of this month'));

你可以用 $date 替换 date("Y-m-d", strtotime($date)),因为它们是相同的,但你可以写更少的代码。 - nicolascolman

4

获取上个月的第一天和最后一天:

$dateBegin = strtotime("first day of last month");  
$dateEnd = strtotime("last day of last month");

echo date("D-F-Y", $dateBegin);  
echo "<br>";        
echo date("D-F-Y", $dateEnd);

2

简单格式

   $curMonth = date('F');
   $curYear  = date('Y');
   $timestamp    = strtotime($curMonth.' '.$curYear);
   $first_second = date('Y-m-01 00:00:00', $timestamp);
   $last_second  = date('Y-m-t 12:59:59', $timestamp); 

下个月的更改将把 $curMonth 更改为 $curMonth = date('F',strtotime("+1 months"));


1
我有一个最简单的解决方案。
$startDate = date("Y-m-d",strtotime("Jan")); //2023-01-01
$endDate = date("Y-m-t",strtotime("Jan")); //2023-01-31

对于月份,您可以使用data('M')作为strtotime()函数的参数。


t代表“给定月份的天数” - Pascal

0
$month=01;
$year=2015;
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo $num;

显示过去31天的日期


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,