1

how would I go about echo'ing the first and last day of the current month? I assume I would use mktime() but I am slightly confused by it.

For example the current month is June so I am looking for a way to echo:

2016-06-01 00:00:00 

And

2016-06-30 00:00:00

Thanks in advance.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Ryan
  • 161
  • 2
  • 14
  • http://stackoverflow.com/questions/2094797/the-first-day-of-the-current-month-in-php-using-date-modify-as-datetime-object – Akhilesh B Chandran Jun 06 '16 at 11:01
  • Why would you use mktime? Both DateTime and strtotime() can give you the first day of this month, and from that it's easy to get the last day of the month – Mark Baker Jun 06 '16 at 11:04

3 Answers3

2

You can use DateTime for this.

<?php

    $start_date_month = new DateTime("first day of last month");
    $end_date_month = new DateTime("last day of last month");

    echo $start_date_month->format('Y-m-d'); 
    echo $end_date_month->format('Y-m-d');
?>
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
1

Here is one method:

select date_sub(curdate(), interval 1 - day(curdate()) day) as month_start,
       date_sub(date_add(curdate(), interval 1 month), interval - day(curdate()) day) as month_end
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1
SELECT ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)),1) first, 
       LAST_DAY(CURDATE()) last
splash58
  • 26,043
  • 3
  • 22
  • 34
  • Could you maybe explain a bit what you code does, and how it works ? That would make it more helpful to future visitors. – Jonas Czech Jun 06 '16 at 12:28