Jump to content

MySQL last day of month


Go to solution Solved by Barand,

Recommended Posts

Hi,

Is it possible to get the last date of a month in mysql on a start date variable?

 

Example...

WHERE date >= '$date' and date <= GETLASTDAY('$date'):

I'm aware this wont work - but may help understand what i'm trying to achieve.

I currently pass in a variable called $date which has a value of "2013-11-01" . I'm hoping to execute a mysql query where it would figure out the last possible date in that month based on the $date variable e.g '2013-11-30'.

 

In theory i want to execute something like the below - but it automagically figure out the second value

WHERE date >= '2013-11-01' and date <= '2013-11-30':

Is there an easy way to do this?

 

Thanks,

 

MoFish

Link to comment
https://forums.phpfreaks.com/topic/284359-mysql-last-day-of-month/
Share on other sites

  • Solution
$date = '2013-11-01';
$last = date('Y-m-t', strtotime($date));
echo "$date - $last"; // --> 2013-11-01 - 2013-11-30

or you could do

 

WHERE EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM '$date')

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.