There are a lot of questions that ask about 'UNIX timestamp to MySQL time'. I needed the reversed way, yea... Any idea?
Answers
This is best done on the client side in the presentation layer. Here is a JS solution:
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
Timeago will turn all abbr elements with a class
of timeago and an ISO 8601 timestamp in the title
:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
into something like this:
<abbr class="timeago" title="July 17, 2008">about a year ago</abbr>
To convert the date into the ISO 8601 format you can do something like this:
<?= date("c", $post_date) ?>
Examples:
You opened this page less than a minute ago. (This will update every minute. Wait for it.)
This page was last modified 11 days ago.
Ryan was born 31 years ago.
You can use the DateTimeZone
class:
$gmt = new DateTimeZone("GMT");
$datetimeInGMT = new DateTime($now, $gmt);
It also takes locations in the form continent/city
, e.g. Europe/London
.
If your datetime is non-UTC, you can use setTimezone
:
$datetimeInGMT = new DateTime($now, new DateTimeZone("America/New_York"));
$datetimeInGMT->setTimezone(new DateTimeZone("GMT"));
$mysqlDateTime = "2012-07-30 10:12:39";
If you just need the date from that, you can use something like this (easy) one:
list($date, $time) = explode(" ", $mysqlDateTime);
echo $date;
However, you may use also something like this:
echo date("Y-m-d", strtotime($mysqlDateTime));
EDIT
The solution of donald123 may also be interesting for you.
mysql> select unix_timestamp('2008-01-08 19:23:32');
+---------------------------------------+
| unix_timestamp('2008-01-08 19:23:32') |
+---------------------------------------+
| 1199849012 |
+---------------------------------------+
1 row in set (0.04 sec)
found here: http://www.epochconverter.com/
Use
strtotime(..)
:Also check this out (to do it in MySQL way.)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp