Ryan
Ryan

Reputation: 161

How to print current month in Mysql DateTime format in PHP

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.

Upvotes: 1

Views: 255

Answers (3)

splash58
splash58

Reputation: 26153

SELECT ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)),1) first, 
       LAST_DAY(CURDATE()) last

Upvotes: 1

Alok Patel
Alok Patel

Reputation: 8022

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

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269743

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

Upvotes: 1

Related Questions