MySQL Date_Format based on today's date and another column? - php

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.

Related

Laravel - How to check current month inside an date string?

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();

How can I get all records for the week using date strings not date objects?

I have a table in the database where the records contain a start_date and an end_date. An example of the data format is:
"13/03/2020"
How can I fetch all records for the week? I have tried the code below. However, it does not return any data.
$date = Carbon::now()->format('d/m/Y');
$dateInWeek = Carbon::now()->addDays(7)->format('d/m/Y');
$holidays = Holiday::where('start_date', '>=', $date)
->where('end_date', '<=', $dateInWeek)->get();
Probably correct solution is to change string to datetime in db, if it is not an opinion then you can create custom function in database that will handle strings in way you want.
You shouldn't use string when you can use date / datetime column type.
But that being said - it's not always up-to us to be able to change DB structure.
Solution 1
Depending on what SQL you are using there is usually function to cast string to date.
For example MySQL: STR_TO_DATE(string, format)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date
Solution 2
You can make array containing all dates of week using Carbon.
then in query: ->whereIn('start_date', $datesArray)->whereIn('end_date', $datesArray)
I'd still suggest changing column type to date if possible

How to select last 7 days results from mysql (with custom data field)?

I need to select data from mysql for last 7 days. I have field named 'date' and which have values in mm.dd.yy format.
So i tried to find special mysql request to do that, but its not work with my field, i gues that beacause date in wrong format.
How i can do that from php (use some variable to get mysql entries), or with custom select query ?
You can use STR_TO_DATE() to convert your idiosyncratic date format to a standard DATE value. An expression like this will do the trick
STR_TO_DATE('07.17.97', '%m.%d.%y')
Then you can say
WHERE STR_TO_DATE(`date`, '%m.%d.%y') >= CURDATE() - INTERVAL 7 DAY
in your query to filter items with date values starting a week ago.
But, if you have a lot of rows to filter you will have poor performance: this kind of WHERE clause is not sargable.
First read your table and change the date format
$new_date_format = date('Ymd',mktime(0,0,0,substr($date,0,2),substr($date,3,2),substr($date,6,2)));
After that you can make comparisions

what is the format with which mysql stores date and time?

While creating a table, I defined one column of DATE type and one of TIME type. As I insert the values using a php script like :
date--> 2013-11-11
time--> 12:12:12
and when I query the sql browser I see those values in exactly the same manner. But I am unaware of the format with which it stores the date and time. Like yyyy-mm-dd or yyyy-dd-mm.
Is there any way I change it ?
Dates and times are stored in MySQL in the format "YYYY-MM-DD" and "YYYY-MM-DD HH:MM:SS" which is not necessarily the format you want to display in your web page or application. There are two methods to reformat the date and time into the desired format. One is to do it in the SQL query in MySQL calling the DATE_FORMAT() function and the other is to do it with the programming language retrieving the data from the MySQL database.
From MySQL 5.1:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
For second question: you can't change default DATE format for the storage, please see this question also
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format
http://dev.mysql.com/doc/refman/5.5/en/time.html
MySQL retrieves and displays TIME values in 'HH:MM:SS' format
I do not believe this can be changed. But you do not care. You can extract dates and times in the format of your liking with the DATE_FORMAT() and the TIME_FORMAT() functions.
If you want to know the internal storage of Date columns, you can check Date and Time Data Type Representation, but I think you want to select date in different format; which other guys already answered about it.
It is stored in 3 bytes, and it is always YYYY-MM-DD.
The datetime is in Y-m-d H:i:s format, or year month day and hour minute second. If you only use a part, the format stays the same.
If you want to change the format there are many ways. The easiest would be to do something like return date("Y-d-m H:i:s", strtotime($mysqldatetime)); (will turn it to dutch date);
Keep in mind that you are using two seperate columns, one for time and one for the date. If you use only one column the missing values are filled with default values (time would be 00:00:00 and date would be 1970-01-01

how do i get day only from javascript calender and store it into database

I had used javascript calender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.
How can I do that as I cannot make changes in javascript code as i m not good at it.
$date_customer=date("d",strtotime($_POST['datum1']));
I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.
Instead of sending date to server you could send the day by using
.getDay() method of javascript Date object.
I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy
So your php will need to be the following to only get the day
$date = explode("/",$_POST['datum1']);
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0];
// otherwise if format is mm/dd/yyyy then use the below
$date_customer = $date[1];
Check out the explode function
i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .
the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,
then in php using the explode function and create the unix timestamp using the mktime function
then save it to the database as an int ,
then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...
cheers
I suppose that you use mysql (TINYINT are mysql specific).
TINYINT are integer and in php integer are usually not prefixed by zeros
Use a DATE field and format it when you report it.
You can use mysql date_format(date,"%d") function in your query.
Or the php date function.
select date_format(date_field,"%d") from some_table;
You can use a VARCHAR(2) to store the date (ugly).
If you stick to store only the DAY in an int or tynint.
use sprintf("%02d",$day) to format it correctly in php.

Categories