Monday, February 27, 2012

Convert Unix Timestamp To MySQL Timestamp

PHP date functions such as strtotime() and time() and date() all return a value as a UNIX TIMESTAMP. This is great for date manipulation but becomes a problem when we need a date to INSERT into a MySQL database. The format of a MySQL TIMESTAMP is yyyy-mm-dd hh:mm:ss and to meet this need a simple PHP can be utilized to convert any UNIX TIMESTAMP into a MySQL TIMESTAMP.

Code and use of function is:

[php]
<?php

/**
*
* @convert UNIX TIMESTAMP to MySQL TIMESTAMP
*
* @param int $timestamp
*
* @return string
*
*/
function unixToMySQL($timestamp)
{
return date('Y-m-d H:i:s', $timestamp);
}


/*** example usage ***/
$time = time();
echo unixToMySQL($time);

?>

[/php]

6 comments:

  1. Thanks for the information, I hardly ever like it.

    ReplyDelete
  2. You have mentioned very interesting details! ps nice web site.

    ReplyDelete
  3. Some really nice and useful info on this website, likewise I think the design holds good features.

    ReplyDelete
  4. I just want to mention I am just newbie to blogging and site-building and truly liked your page. Likely I’m planning to bookmark your site . You actually come with exceptional posts. Thanks a bunch for sharing your website page.

    ReplyDelete
  5. I simply want to say I am newbie to blogging and really enjoyed your blog. Almost certainly I’m want to bookmark your blog . You actually come with perfect articles and reviews. Many thanks for revealing your blog site.

    ReplyDelete
  6. along with the new facebook message im not receiving enymore emails when someone sents me a message..why is this occurring?

    ReplyDelete

Note: Only a member of this blog may post a comment.