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

11 Answers 11

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));
6
  • ty: your approach is smarter. No need for explode.
    – dole doug
    Commented Sep 25, 2011 at 6:06
  • 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.
    – dole doug
    Commented Sep 25, 2011 at 7:40
  • The "d-m-Y" format is supported. The supported formats are listed at http://ca2.php.net/manual/en/datetime.formats.date.php. Commented Sep 25, 2011 at 17:19
  • I can't find where the "t" value is defined. Could you point me in the right direction?
    – Steven
    Commented Dec 5, 2012 at 21:12
  • I've pretty much searched the whole web for so many times. This is an ingenious solution. Thanks Francois.
    – manju4ever
    Commented Nov 24, 2015 at 9:00
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"));
2
  • 2
    This solution is wrong, because the question was about a "given month", but your answer only works for the current month. Commented Nov 27, 2020 at 10:24
  • Replacing "this month" by desired month (like $date->format('F') or (new \DateTime('2010-02-04')->format('F') in this case) does the trick to
    – Skunka
    Commented Dec 7, 2021 at 9:21
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..

8

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

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. Commented Sep 24, 2011 at 21:54
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')));
3
$month = 10; // october

$firstday = date('01-' . $month . '-Y');
$lastday = date(date('t', strtotime($firstday)) .'-' . $month . '-Y');
2

Basically:

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

Date t parameter return days number in current month.

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);
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));
1
  • 3
    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. Commented Aug 9, 2019 at 15:38
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:

-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"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.