So, I have a field on my database that keeps the date, just like the created_at field, year-month-day format.
I need to create a method to do a query to return all results where month = current month we are living. How can I check this on laravel?
Thanks in advance!
Model::whereMonth('field', Carbon::now()->month)->get();
Related
I'am using a script that saves some fields into my database as Epoch & Unix Timestamp format, (I think because the field is submitting as something like 1515469971), the column is an "int" format type.
I want to know, how I can make a query, to search all the inputs that have created on the current month.
I am so confused about it, hope you can help me :)
Firstly, show me your table structure with show create table table_name.
If you're using an int to store the time. There are two ways to do that:
Convert the field to a date string with from_unixtime. Then you got a string like '2018-01-08 05:05:05', and just compare the month value.
Calculate the very beginning and the end of the current month in advance. Then just select * from table_name where time >= beginning_of_the_month and time <= end_of_the_month.
How can I retrieved records from the database using carbon dates with the specified date range e.g retrieved records from "2014-10-13" to "2015-11-18"? any ideas, help, suggestions, clues, recommendations please?
You can use the whereBetween method. So, just as an example, lets say you have two Carbon dates.
$earlier = Carbon::yesterday();
$later = Carbon::today();
Model::whereBetween('column', [$earlier, $later])->get();
The first parameter represents the column you are checking. The second parameter is an array of the two dates.
Using the dates in your question, you can generate the carbon dates like this:
$earlier = Carbon::parse('2015-09-11');
$later = Carbon::parse('2015-09-14');
Model::whereBetween('column', [$earlier, $later])->get();
The parse method is pretty awesome.
I have at table where I store all events. In that table I have a column named "event_date". The person who creates a event has to type the date like this:d/m/Y - example: 27/02/2013.
But when I try to sort the results by the event_date ASC, it doesn't come out in the desired order. I have even tried ordering it with UNIX_TIMESTAMP(event_date), but it doesn't solve my problem either.
How can I fix this?
There is one solution; when a person creates an event, I can convert it to a timestamp. But isn't it possible to sort it correctly as it is now?
Thanks in advance.
You would have to store your dates as YYYY/MM/DD in your VARCHAR column to have an alphanumeric sort work properly.
As it is, the system will sort:
27/02/2013
01/03/2013
06/03/2013
like this:
01/03/2013
06/03/2013
27/02/2013
If you can change your column to the DATE data type, you can either switch your input format to YYYY-MM-DD or continue to accept DD/MM/YYYY and use:
STR_TO_DATE('01/03/2013','%d/%m/%Y')
to convert the value to a valid date
I am developing a holiday guide site!
I am using a MySQL database in which I store the information about hotels.
I have 4 date fields in order to store the 2 possible seasons that works each hotel (date_from, date_to, date_from_extra, date_to_extra). I store the dates in a format like this: DD/MM, for example: 25/12.
I am trying to check if the date1 of first datepicker of the site and the date2 of the second datepicker are between first range (date_from, date_to) or between second range (date_from_extra, date_to_extra) in the database.
The problem is that I don't use years in database date fields and is difficult to compare dates especially when the first or second season in database doesn't finish at the end of the year but it lasts more, for example (14/11 - 25/02).
Any help?
Thanks a lot!
try this
$todayDate = date("Y-m-d");
$check_date= strtotime($todayDate);
use this varible to check
SELECT table_fiels FROM table_namE WHERE month(check_date) BETWEEN month($your_db_start_date) AND month($your_db_end_date) AND day(check_date) BETWEEN day($your_db_start_date) AND day($your_db_end_date)
I am aware of the MySQL Date_Format function but am looking to achieve the following:
I have on column with a day date in 2 digit format (01-30). I am trying to update another date formatted field with the current year, the next month (m+1) and the day field mentioned previously.
In PHP i would do this using mktime function but this must be done using mysql calls only.
Is it possible to transform in this way?
update table set field1 = concat(date_format(curdate(),"%Y-%m"),'-',field2) + interval 1 month
There is a function called STR_TO_DATE in mysql which you should be able to use to create a brand new date with using the seperate parts you described in your problem. The final input into the function should be STR_TO_DATE('01,5,2013','%d,%m,%Y') where argument one is the actual date string and argument two represents the format of the new date. You should be able to create the first argument by concatenating your parts together and then specifying whatever date format you need.