如何在一个月内使用PHPfind第一个和最后一个date?

如何在一个月内使用PHPfind第一个和最后一个date? 例如,今天是2010年4月21日; 我想find2010年4月1日和2010年4月30日。

最简单的方法是使用date ,它可以将硬编码的值与从时间戳中提取的值混合使用。 如果您没有给出时间戳,则会假定当前的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('mt-Y'); // With timestamp, this gets last day of April 2010 $last_day_april_2010 = date('mt-Y', strtotime('April 21, 2010')); 

date()search给定的string,例如'mt-Y' ,查找特定符号,并用时间戳中的值replace它们。 所以我们可以使用这些符号从时间戳中提取我们想要的值和格式。 在上面的例子中:

  • Y从时间戳('2010')给出4位数的年份
  • m给你从时间戳的数字月份,前导零('04')
  • t给你在时间戳的月份(“30”)的天数

你可以用这个创意。 例如,要获得一个月的第一个和最后一个秒:

 $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('mtY 12:59:59', $timestamp); // A leap year! 

有关其他符号和更多详细信息,请参阅http://php.net/manual/en/function.date.php

简单的一个

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

参考 – http://www.php.net/manual/en/function.date.php

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

您可以使用date函数来查找一个月中有多less天。

 // 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. 

希望有所帮助。

编辑:在阅读路易斯的答案后,它发生在我身上,你可能需要它在正确的格式(年 – 月 – 日)。 这可能是显而易见的,但不要提及:

 // After the above code echo date('Ym-t', $ts); 

这会给你这个月的最后一天:

 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('Ym-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('Ym-d', $result); } 

对于特定的月份年份,使用date()作为自然语言,如下所示

 $first_date = date('dm-Y',strtotime('first day of april 2010')); $last_date = date('dm-Y',strtotime('last day of april 2010')); // Isn't it simple way? 

但本月

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

如果你想从指定的datevariables中find第一天和最后一天,那么你可以像下面这样做:

 $date = '2012-02-12';//your given date $first_date_find = strtotime(date("Ymd", strtotime($date)) . ", first day of this month"); echo $first_date = date("Ymd",$first_date_find); $last_date_find = strtotime(date("Ymd", strtotime($date)) . ", last day of this month"); echo $last_date = date("Ymd",$last_date_find); 

对于目前的date只是简单的使用这个

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

现场演示

获取上个月的第一个和最后一个date;

 $dateBegin = strtotime("first day of last month"); $dateEnd = strtotime("last day of last month"); echo date("DFY", $dateBegin); echo "<br>"; echo date("DFY", $dateEnd); 
 $month=01; $year=2015; $num = cal_days_in_month(CAL_GREGORIAN, $month, $year); echo $num; 

显示31date的最后一天

以简单的格式

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

下个月将$ curMonth更改 $ curMonth = date('F',strtotime(“+ 1 months”));