Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert current timestamp into mysql with php?

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?

like image 269
Rafael Adamy Avatar asked Apr 13 '12 03:04

Rafael Adamy


People also ask

How can I insert current date and time in MySQL using PHP?

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().

How do I add a timestamp in MySQL?

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 insert a timestamp into a SQL table?

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.

What is current timestamp in MySQL?

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.


1 Answers

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

like image 72
leepowers Avatar answered Oct 06 '22 20:10

leepowers