JasonDavis
JasonDavis

Reputation: 48963

What MySQL column type to save the value of PHP's time() function result?

I have a PHP PDO Database Class that I am trying to modify and use for my needs.

One of the features it has is the option to auto-update created_+at and updated_at DB columns.

This is similar to Laravel as models in Laravel have the option of doing this. Luckily though Laravel handles creating the DB columns for you.

IN my case I have to create these columns and the PHP class will handle updating them.

The class uses the PHP time() function to update these columns.

After some reasearch to see what type of columns Laravel uses for its timestamp created_at and updated_at columns I found it uses a MySQL timestamp column type.

So I proceeded to test my PDO class with a DB table that has a created_at column of the timestamp type with this query below and got error message:

INSERT INTO `timeclock`.`password_reminders` 
    (`email`, `token`, `created_at`) 
VALUES 
    ('[email protected]', '5432', '1455771891'),
    ('[email protected]', '12345', '1455771898');

I get this message in phpMyAdmin:

2 rows inserted.
Warning: #1265 Data truncated for column 'created_at' at row 1
Warning: #1265 Data truncated for column 'created_at' at row 2

The PHP class uses time() to generate values like this: value like this:1455771305``

So what type of DB column should I use for these fields?

Upvotes: 0

Views: 183

Answers (2)

chandni patel
chandni patel

Reputation: 1

using time function insert time in mysql column

mysql_select_db('ecommerce', $con);

$time= time('h:i:s A');

mysql_query('INSERT INTO tbl_users (id,firstName,lastName,time) VALUES (35,"asd","pqr", "'.$time.'")');

Upvotes: 0

Dylan Su
Dylan Su

Reputation: 6065

Replace the integer string '1455771891' with from_unixtime('1455771891').

mysql> select from_unixtime('1455771305');
+-----------------------------+
| from_unixtime('1455771305') |
+-----------------------------+
| 2016-02-18 12:55:05.000000  |
+-----------------------------+
1 row in set (0.00 sec)

Upvotes: 1

Related Questions