代码之家  ›  专栏  ›  技术社区  ›  Karthik

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

  •  71
  • Karthik  · 技术社区  · 15 年前

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

    9 回复  |  直到 8 年前
        1
  •  185
  •   Nathan Long    12 年前

    最简单的方法是 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 给你从时间戳('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!
    

    http://php.net/manual/en/function.date.php 其他符号和更多细节。

        2
  •  23
  •   Siva Kranthi Kumar    15 年前

    简单一

    • 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 />';
    ?>
    
        3
  •  16
  •   Henrik    15 年前

    您可以使用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); 
    
        4
  •  10
  •   Luis    15 年前

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

    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);
    } 
    
        5
  •  3
  •   Jimil Choksi    10 年前

    针对具体 使用日期()作为自然语言,如下所示

    $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
  •  3
  •   Faisal    8 年前

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

    $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'));
    

    Live Demo

        7
  •  2
  •   Shehary    10 年前

    得到的第一个和最后一个日期 Last Month ;

    $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);
    
        8
  •  2
  •   Yasar Arafath    8 年前

    简单格式

       $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=日期('f',strotime(“+1个月”);

        9
  •  0
  •   sunakshi verma    9 年前
    $month=01;
    $year=2015;
    $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    echo $num;
    

    显示31日期的最后一天

    推荐文章