1
votes

i have a table which structure contains lots of fields and one field 'booking_time' of type datetime. E.g. values of booking_time are 2015-10-04 07:33:45, 2016-07-20 17:40:00 and so on...

In my code i recieve the month and the year as two serperate integer variables (e.g 1 to 12 for months and 2015, 2016, etc. for the year). Now i need to get all table entries which have the matching year and month in the booking_time field.

How do i set up my mysql-query string to get the two numbers from the variables into the datetime format of mysql and then compare it with booking_time to get all the entries that correspond?

EDIT: My problem is the comparing of the two numbers with a mysql datetime field and not that i don't know anything about mysql queries.

Kind Regards

1
Yes. My Problem lies more into the comparing only the year and the month with a full datetime structure. - user6629162
You can pull in Carbon which easily does all this for you (unless you want to manually do it). - Matt Inamdar
@MubeenInamdar nice hint. But not what i need in this situation - i think i wrote my question too unclear. But after having a look at Carbon, i will definetly bookmark it for future projects. - Juarrow

1 Answers

0
votes

Go with something like that:

SELECT * from the_table where MONTH(booking_time) = 10 AND YEAR(booking_time) = 2016

This would select anything from October 2016. Make sure to compare that and month, or you will get multiple years at some day.