This is driving me crazy. I've searched it on interent but can't get to a conclusion.
I have a date field in my database. Which I'm retrieving values with date function just fine.
Problem is I did it some time ago, and I don't remember how I inserted those values into that field. it's an integer value (according to what I read, the number of seconds since jan 1st 1970, for example 1338949763. but when I try inserting stuff, like using INSERT INTO entries (date) VALUES (NOW())
. The NOW
thing will store a formatted date, like 2012-04-12 23:09:54
. I don't want that. I want to store the integer value. how do I do that?
To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().
To demonstrate, let's first create a table named as Sample with sample_ts as a TIMESTAMP field. CREATE TABLE Sample ( sample_id int NOT NULL, sample_name VARCHAR(20), sample_ts TIMESTAMP ); Next, you have to set the timezone to '+00:00' UTC by issuing the SET time_zone command. SET TIME_ZONE = '+00:00';
How do I add a TIMESTAMP to a MySQL table? Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.
MySQL CURRENT_TIMESTAMP() Function The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.
Use MySQL's UNIX_TIMESTAMP()
function to get the current epoch time:
INSERT INTO mytable SET tstamp = UNIX_TIMESTAMP();
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With