Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:47:56+00:00 2026-06-12T15:47:56+00:00

I am trying to save a time in a MySQL table. However, the date

  • 0

I am trying to save a time in a MySQL table. However, the date reverts to 2000-01-01.

1.9.2p320 :036 > vv = Visitor.new
 => #<Visitor id: nil, ip_address: nil, num_day_visits: nil, last_visit: nil> 
1.9.2p320 :037 > vv.last_visit = Time.now; vv.ip_address = "3.3.3.3"
 => "3.3.3.3" 
1.9.2p320 :038 > vv.num_day_visits = 1
 => 1 
1.9.2p320 :039 > vv
 => #<Visitor id: nil, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :040 > vv.save
  SQL (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO `visitors` (`ip_address`, `last_visit`, `num_day_visits`) VALUES (?, ?, ?)  [["ip_address", "3.3.3.3"], ["last_visit", 2012-10-11 01:31:04 -0400], ["num_day_visits", 1]]
   (0.5ms)  COMMIT
 => true 
1.9.2p320 :042 > vv
 => #<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :043 > Visitor.find(:all,:conditions=>{:ip_address => "3.3.3.3"})
  Visitor Load (1.4ms)  SELECT `visitors`.* FROM `visitors` WHERE `visitors`.`ip_address` = '3.3.3.3'
 => [#<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2000-01-01 05:31:04">] 

So when I retrieve the record, the date is 2000-01-01.

The table in MySQL:

mysql> describe visitors ;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| ip_address     | varchar(255) | NO   |     | NULL    |                |
| num_day_visits | int(11)      | NO   |     | NULL    |                |
| last_visit     | time         | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Update: I created a toy table to play around the conversion from time to datetime. This is what happens:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  1 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> alter table example change mytime mytime datetime;
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> select * from example ;
+----+---------------------+
| id | mytime              |
+----+---------------------+
|  1 | 0000-00-00 00:00:00 |
+----+---------------------+
1 row in set (0.00 sec)

So that destroys the value. I tried to go back by altering the table to time, and using a new row. Starting over:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  2 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP();
ERROR 1067 (42000): Invalid default value for 'mytime'
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_DATE();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_DATE()' at line 1

Apparently, this is hard.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:47:57+00:00Added an answer on June 12, 2026 at 3:47 pm

    This is because your last_visit column is of the time and not datetime type. Initially I assumed that mysql just uses 01-01-2000 as some sort of default date for representing time fields internally, however it appears that 01-01-2000 is the doing of rails and not mysql. Look at Represent a time with no date in ruby

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to enter the date and time into datetime field in MySql.
I am trying to save date value to mysql DB, DB date column has
Recently I've been trying to save on render time by minimizing the amount of
I'm trying to save myself some coding time by taking advantage of using namespace
I'm trying to build a little renaming program to help save me time in
I'm having hard time trying to figure out how to auto-save user data in
im trying to save a row in my settings table, it works all fine
I'm using JPA2 with EclipseLink implementation. I'm simply trying to save the current Date
In trying to save as much time as possible in my development and make
I'm trying to make my SQL Server table datetime columns save datetime with AM/PM.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.