MySQL Date & Time functions
There is exclusive fields type to store Date and Date –Time in mysql or in other databases. As we know in any script date plays important role and we need to do many calculation and formatting to handle the date issues. It can be a simple Library book issue management to a customer compliant handling, these date fields are important for handling related data in a table.
MySQL have several date & time related functions to use in different situations and requirements. We will discuss some of the important functions here and use them for developing some sample codes.
You can read php date & time functions.
DEMO of Date Functions
The person will retire on the last working day of the retirement month.
Getting records of each day by using group by and count command in mysql date field
Different Financial Quarter data
Date fields of MySQL table accept a particular format of Date & time strings, so we have to create such an acceptable format while storing Date values.
We can use str_to_date function to format date value stored as string in a varchar field ( not date field )
We can store the present date & time in a DateTime field while updating adding a record by using default value to CURRENT_TIMESTAMP
We can format the returned date value from table by using strtotime function.
based on user inputs of date from two calendars
Exercise on DATE queries Practice Questions
Retrieve the current date.
Get the current time.
Calculate the number of days between '2023-06-15' and today's date.
Display the year of the date '2023-08-03'.
Calculate the age (in years) of a person born on '1995-05-20'.
Show the month of the date '2023-08-03'.
Find the day of the week for '2023-08-03'.
Format the current date and time as 'August 3, 2023'.
Add 2 months to the date '2023-08-03'.
Calculate the time difference in hours between '2023-08-03 10:00:00' and '2023-08-03 14:30:00'.
Display the current date and time in the 'YYYY-MM-DD HH:MM:SS' format.
Subtract 5 years and 3 months from the current date.
Calculate the number of weekdays (non-weekend days) between '2023-08-01' and '2023-08-15'.
Get the start of the month for the date '2023-08-03'.
Find the date 2 weeks ago from today.
Subscribe to our YouTube Channel here
This article is written by
a really good work. one of the best sites that gives u exact working answers
how to insert current time in mysql for automatic using php??