
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL - CURTIME() Function
The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively. Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.
The MYSQL CURTIME() function is used to get the current time. The resultant value is a string or a numerical value based on the context and, the time returned will be in the 'hh:mm:ss' or hhmmss format.
Syntax
Following is the syntax of the above function –
CURTIME();
Example 1
Following example demonstrates the usage of the CURTIME() function –
mysql> SELECT CURTIME(); +-----------+ | CURTIME() | +-----------+ | 17:50:10 | +-----------+ 1 row in set (0.00 sec)
Example 2
Following is an example of this function in numerical context –
mysql> SELECT CURTIME()+0; +-------------+ | CURTIME()+0 | +-------------+ | 175033 | +-------------+ 1 row in set (0.00 sec)
Example 3
You can add seconds to the current time as shown below –
mysql> SELECT CURTIME()+12; +--------------+ | CURTIME()+12 | +--------------+ | 175124 | +--------------+ 1 row in set (0.00 sec)
Example 4
We can also subtract the desired number of seconds from the current time using this function –
mysql> SELECT CURTIME()-22213; +-----------------+ | CURTIME()-22213 | +-----------------+ | 152933 | +-----------------+ 1 row in set (0.00 sec)
Example 5
Let us create another table with name Sales in MySQL database using CREATE statement as follows –
mysql> CREATE TABLE sales( ID INT, ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DispatchTime time, Price INT, Location VARCHAR(255) ); Query OK, 0 rows affected (2.22 sec)
Now, we will insert 5 records in Sales table using INSERT statements −
insert into sales values (1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad'); insert into sales values (2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam'); insert into sales values (3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada'); insert into sales values (4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai'); insert into sales values (5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
Following is another example of this function —
mysql> SELECT ProductName, CustomerName, DispatchDate, Price, DATEDIFF(CURRENT_DATE, DispatchDate) as difference_in_days, TIMEDIFF(CURTIME(), DispatchTime) as time_difference FROM sales; +-------------+--------------+--------------+-------+--------------------+-----------------+ | ProductName | CustomerName | DispatchDate | Price | difference_in_days | time_difference | +-------------+--------------+--------------+-------+--------------------+-----------------+ | Key-Board | Raja | 2019-09-01 | 7000 | 679 | 06:52:52 | | Earphones | Roja | 2019-05-01 | 2000 | 802 | 06:52:52 | | Mouse | Puja | 2019-03-01 | 3000 | 863 | 06:52:53 | | Mobile | Vanaja | 2019-03-01 | 9000 | 863 | 07:42:00 | | Headset | Jalaja | 2019-04-06 | 6000 | 827 | 06:43:53 | +-------------+--------------+--------------+-------+--------------------+-----------------+ 5 rows in set (0.00 sec)
Example 6
Suppose we have created a table named SubscribersData with 5 records in it using the following queries –
mysql> CREATE TABLE SubscribersData( SubscriberName VARCHAR(255), PackageName VARCHAR(255), SubscriptionDate date, SubscriptionTime time ); insert into SubscribersData values('Raja', 'Premium', Date('2020-10-21'), Time('20:53:49')); insert into SubscribersData values('Roja', 'Basic', Date('2020-11-26'), Time('10:13:19')); insert into SubscribersData values('Puja', 'Moderate', Date('2021-03-07'), Time('05:43:20')); insert into SubscribersData values('Vanaja', 'Basic', Date('2021-02-21'), Time('16:36:39')); insert into SubscribersData values('Jalaja', 'Premium', Date('2021-01-30'), Time('12:45:45'));
Following query calculates and displays the remaining number of days and time for the subscription to complete —
mysql> SELECT SubscriberName, PackageName, DATEDIFF(CURRENT_DATE, SubscriptionDate) as RemainingDays, TIMEDIFF(CURTIME(), SubscriptionTime) as RemainingTime FROM SubscribersData; +----------------+-------------+---------------+---------------+ | SubscriberName | PackageName | RemainingDays | RemainingTime | +----------------+-------------+---------------+---------------+ | Raja | Premium | 263 | -03:00:17 | | Roja | Basic | 227 | 07:40:13 | | Puja | Moderate | 126 | 12:10:12 | | Vanaja | Basic | 140 | 01:16:53 | | Jalaja | Premium | 162 | 05:07:47 | +----------------+-------------+---------------+---------------+ 5 rows in set (0.05 sec)