top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can the current date and time be calculated using MySQL?

+1 vote
751 views
How can the current date and time be calculated using MySQL?
posted May 20, 2014 by Sachin Dahda

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

If you ever had a close look at the MYSQL official documentation then you would have definitely not missed the following link

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

there are built in functions like CURRENT_TIME() and CURRENT_DATE() which return the time and date in human readable form.

If you are looking for current unix time then you can use UNIX_TIMESTAMP()

answer May 20, 2014 by Shyam Purkayastha
0 votes

For Current Date:
SELECT CURDATE();
CURRENT_DATE()=CURDATE()

For Current Time
SELECT CURTIME();
CURRENT_TIME()=CURTIME()

answer May 21, 2014 by Mohit Sharma
Similar Questions
+2 votes

I have a date field on an html form that users may leave blank. If they do leave it blank I want to write the date 01/01/1901 into the mysql table. How can I accomplish this and where in my .php script file should I put the code?

+1 vote

I have a table in Mysql DB like following

mysql> describe APPTBL;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| APP_ID      | int(11)     | NO   | PRI | NULL    | auto_increment |
| CREATE_DATE | date        | YES  |     | NULL    |                |
| CLOSE_DATE  | date        | YES  |     | NULL    |                |
| ACTION      | text        | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+

While Starting of App, i am inserting the app details with CREATE_DATE and when it is closing, i am updating its CLOSE_DATE. But in case if user is restarting the app, i want to update NULL in place of CLOSE_DATE column. Here is my query,

mysql> update APPTBL set CLOSE_DATE = NULL where APP_ID = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> select * from APPTBL ;
+-----------+-------------+------------+------------+
|   APP_ID  | CREATE_DATE | CLOSE_DATE |   ACTION   |
+-----------+-------------+------------+------------+
|    1      |  2013-10-08 | NULL       | XXXXXYYYYY |
+-----------+-------------+------------+------------+
1 row in set (0.00 sec)

But why i am not getting any result in this??

mysql> select * from APPTBL where CLOSE_DATE = NULL;
Empty set (0.00 sec)

Also, as i am writhing a java program using this where

rs = st.executeQuery("update APPTBL set CLOSE_DATE = NULL where APP_ID = 1");
if(!rs.wasNull()){
   // Why it is coming here??
}
+1 vote

I have a column called name in a table info
So I want to update that with it's current value, like this name + user_input. I tried with this code but not working

mysql_query("UPDATE info SET name = name + '$user_input' WHERE id='$user_id'");

But It returns 0 and update column to 0....

Any idea how to accomplish this task?

...