how to get the first and last days of a given month
Asked Answered
A

11

81

I wish to rewrite a mysql query which use month() and year() functions to display all the posts from a certain month which goes to my function as a 'Y-m-d' parameter format, but I don't know how can I get the last day of the given month date.

$query_date = '2010-02-04';
list($y, $m, $d) = explode('-', $query_date);
$first_day = $y . '-' . $m . '-01';
Aphanite answered 24/9, 2011 at 21:8 Comment(0)
C
188

You might want to look at the strtotime and date functions.

<?php

$query_date = '2010-02-04';

// First day of the month.
echo date('Y-m-01', strtotime($query_date));

// Last day of the month.
echo date('Y-m-t', strtotime($query_date));
Calfee answered 24/9, 2011 at 21:13 Comment(6)
ty: your approach is smarter. No need for explode.Aphanite
what if $query_date comes from php as 'd-m-Y' format: '02-04-2010'? On my tests, it goes fine, but maybe I didn't test all the situations. Do I have to convert to Y-m-d, or strtotime, understand it anyway.Aphanite
The "d-m-Y" format is supported. The supported formats are listed at http://ca2.php.net/manual/en/datetime.formats.date.php.Calfee
I can't find where the "t" value is defined. Could you point me in the right direction?Plainlaid
I've pretty much searched the whole web for so many times. This is an ingenious solution. Thanks Francois.Tubman
t stands for the Number of days in the given month php.net/manual/en/datetime.format.phpPeroxide
R
29

I know this question has a good answer with 't', but thought I would add another solution.

$first = date("Y-m-d", strtotime("first day of this month"));
$last = date("Y-m-d", strtotime("last day of this month"));
Rabb answered 1/11, 2014 at 20:1 Comment(2)
This solution is wrong, because the question was about a "given month", but your answer only works for the current month.Petrapetracca
Replacing "this month" by desired month (like $date->format('F') or (new \DateTime('2010-02-04')->format('F') in this case) does the trick toNee
V
23

Try this , if you are using PHP 5.3+, in php

$query_date = '2010-02-04';
$date = new DateTime($query_date);
//First day of month
$date->modify('first day of this month');
$firstday= $date->format('Y-m-d');
//Last day of month
$date->modify('last day of this month');
$lastday= $date->format('Y-m-d');

For finding next month last date, modify as follows,

 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');

and so on..

Violetavioletta answered 12/11, 2014 at 6:10 Comment(0)
H
8

cal_days_in_month() should give you the total number of days in the month, and therefore, the last one.

Herriot answered 24/9, 2011 at 21:24 Comment(1)
Thanks for sharing. I've never used the Calendar PHP module before. With that said, it's worth pointing out that the module may not be installed, especially on a shared host.Calfee
I
3
// First date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
echo '<br />';
// Last date of the current date
echo date('Y-m-d', mktime(0, 0, 0, date('m')+1, 0, date('Y')));
Impenetrability answered 4/8, 2015 at 12:26 Comment(0)
F
3
$month = 10; // october

$firstday = date('01-' . $month . '-Y');
$lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');
Foment answered 13/10, 2016 at 18:29 Comment(0)
T
2

Basically:

$lastDate = date("Y-m-t", strtotime($query_d));

Date t parameter return days number in current month.

Timberwork answered 24/9, 2011 at 21:14 Comment(0)
B
0

Print only current month week:

function my_week_range($date) {
    $ts = strtotime($date);
    $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
    echo $currentWeek = ceil((date("d",strtotime($date)) - date("w",strtotime($date)) - 1) / 7) + 1;
    $start_date = date('Y-m-d', $start);$end_date=date('Y-m-d', strtotime('next saturday', $start));
    if($currentWeek==1)
        {$start_date = date('Y-m-01', strtotime($date));}
    else if($currentWeek==5)
       {$end_date = date('Y-m-t', strtotime($date));}
    else
       {}
    return array($start_date, $end_date );
}

$date_range=list($start_date, $end_date) = my_week_range($new_fdate);
Bumblebee answered 13/8, 2018 at 0:9 Comment(0)
B
0
## Get Current Month's First Date And Last Date

echo "Today Date: ". $query_date = date('d-m-Y');
echo "<br> First day of the month: ". date('01-m-Y', strtotime($query_date));
echo "<br> Last day of the month: ". date('t-m-Y', strtotime($query_date));
Behah answered 9/8, 2019 at 8:40 Comment(1)
When adding an answer to an older question with existing answers it is useful to explain what new aspect your answer brings to the question. If the passage of time affects the answer acknowledge that as well.Hienhieracosphinx
S
0

Precision-based approach (PHP 7.1+):

If you don't want the ambiguity of string-based manipulations, here's an alternative using DateTime with microsecond-precision:

$query_date = '2010-02-04';
$datetime = DateTime::createFromFormat('Y-m-d', $query_date); // create DateTime obj from 'Y-m-d'

$first_day_of_month = (clone $datetime) // duplicate; don't touch original object
  ->setDate( $datetime->format('Y'), $datetime->format('m'), 1 ) // set to 1st of month
  ->setTime( 0, 0, 0, 0 ); // reset time to 00:00:00.000000

$last_day_of_month = (clone $datetime)
  ->add( new DateInterval( 'P1M' ) ) // forward 1 month
  ->setDate( $datetime->format('Y'), $datetime->format('m'), 1 ) // set to 1st of that month
  ->setTime( 0, 0, 0, -1 ); // reset time to 00:00:00 and then subtract 1 microsecond,
// this yields 23:59:59.999999 of previous month, which is the month we want :)

This gives the 1st of the given month at the very start (00:00:00.000000), and the last of the given month at the very end (23:59:59.999999) in your timezone. (Be mindful of database's timezone being different, in which case, use DateTime::setTimezone for precision)

References:

Smorgasbord answered 6/1 at 17:32 Comment(0)
T
-1

In case of name of month this code will work

echo $first = date("Y-m-01", strtotime("January"));
echo $last = date("Y-m-t", strtotime("January"));
Trefor answered 2/9, 2022 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.