0

I had my query wrong for the first fifty or so entries of a database table and because of this my date field has entries like "January 5th, 2013".

Can someone help me out with a query that will take all the entries in the date field and simply convert them to unix timestamps?

Researched lots of other questions here but nothing I have found seems to address manipulating/converting a group of dates after the fact.

Thanks

3
  • have you taken the date filed as varchar? Commented Feb 6, 2013 at 7:19
  • did you checked this Commented Feb 6, 2013 at 7:20
  • yeah I did unfortunately...can I change the structure then run a query? Commented Feb 6, 2013 at 7:21

4 Answers 4

1

select all rows and use strtotime for getting timestamp value and then update the rows.

Sign up to request clarification or add additional context in comments.

1 Comment

yep...great....just need some help getting out of my one dimensional head when working with this stuff.
1

If you are using php, use "strtotime()" function and update the values in data base.

1 Comment

that is a great and simple suggestion...I often forget that I can easily manipulate the database just by making a quick script also..thanks. "foreach date..convert that to strtotime and update!"
0
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');

read more here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

Comments

0

PHP;

$ts = strtotime("January 5th, 2013");
echo date("Y-m-d", $ts); // 2013-01-05

SQL;

UPDATE `table` SET `date_field` = UNIX_TIMESTAMP(`date_field`);

And, if you want to change column type (I prefer INT type for performance issues);

ALTER TABLE `table` MODIFY `date_field` INT UNSIGNED NOT NULL;

But after this, you need to insert all data to this column as integer, so;

$time = time(); // or $time = strtotime("January 5th, 2013");
INSERT INTO `table` (`date_field`) VALUES ($time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.