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

Kar*_*hik 75 php date

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

Nat*_*ong 196

最简单的方法是使用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'));
Run Code Online (Sandbox Code Playgroud)

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

  • Y 为您提供时间戳('2010')的4位数年份
  • 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!
Run Code Online (Sandbox Code Playgroud)

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

  • 只是在这里添加信息,我在使用这种格式将日期转换回unix时间戳时遇到问题,已通过使用'dmY'格式而不是'mdY'来解决。.最后一秒将是'23:59:59'而不是'12 :59:59'.. (3认同)

Siv*_*mar 25

简单的一个

  • 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 />';
?>
Run Code Online (Sandbox Code Playgroud)


Hen*_* L. 16

您可以使用日期功能查找一个月中的天数.

// 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.
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

编辑:在读完路易斯的回答之后,我想到你可能想要它的格式正确(YY-mm-dd).这可能是显而易见的,但不要提及:

// After the above code
echo date('Y-m-t', $ts); 
Run Code Online (Sandbox Code Playgroud)


Lui*_*uis 11

这将为您提供该月的最后一天:

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);
}
Run Code Online (Sandbox Code Playgroud)

第一天:

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);
} 
Run Code Online (Sandbox Code Playgroud)


小智 6

对于特定的月份年份,使用 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?
Run Code Online (Sandbox Code Playgroud)

但对于当月

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


Fai*_*sal 5

如果要从指定的日期变量中查找第一天和最后一天,则可以执行以下操作:

$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);
Run Code Online (Sandbox Code Playgroud)

对于当前日期只是简单地使用这个

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