Viewed   115 times

There are a lot of questions that ask about 'UNIX timestamp to MySQL time'. I needed the reversed way, yea... Any idea?



Use strtotime(..):

$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);

Also check this out (to do it in MySQL way.)

Thursday, November 17, 2022

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) ?>


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.

Wednesday, August 24, 2022

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"));
Tuesday, November 22, 2022
$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));


The solution of donald123 may also be interesting for you.

Saturday, October 1, 2022
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:

Sunday, August 28, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :