0

I'm a web dev beginner and got a problem with a timestamp and need your help!
I'm working on a posting formula and a posting displayer. In the formula, I save the posting timestamp in "seconds" into the database.
If the user clicks on a button, more information will show up, also the "creation date".
The user do have the choice via a date configuration how the date will get displayed, for example month/day/year or day.month.year, ...
That is the reason why I save the timestamp in seconds to the database and not directly in a date like: month/day/year

$tm = time();

So my ajax formula must fetch the timestamp in seconds from the database and transform it to a date on the fly.
I found in this forum a solution how to transform seconds to a date via javascript but I don't know how to implement it to ajax.

With my code I dont have problems to fetch information from database, it works perfectly fine, but I only know how to fetch the date in seconds, like: 1538395461

Here is a shorter version of my code

AJAX CODE

<script>
var buttons = document.querySelectorAll("a.img_title");


for(var x=0; x < buttons.length; x++)
{
  buttons[x].addEventListener('click', loadDescr);
}

function loadDescr(e)
{
  var abc = e.target.getAttribute('data-xy');

  var xhr_ap = new XMLHttpRequest();
  xhr_ap.open('GET', 'includes/ajax/ajax_posts_conn.php?ref_desr=' + abc, true);

  xhr_ap.onload = function()
  {
    if (this.status == 200)
    {
      /*----------------------------------------------------------------------*/
      var post_header = JSON.parse(this.responseText);
      /*----------------------------------------------------------------------*/
      var output = '';

      for (var i in post_header)
      {
        output += '<ul>' +
          '<li><a>created: '+post_header[i].post_created+'</a></li>' +
          '</ul>';
      }
      document.getElementById('post_creation_date').innerHTML = output;
      /*----------------------------------------------------------------------*/
    }
  }
  xhr_ap.send();
}
</script>

"post_created" is the name of the table row.
"post_creation_date" is the name of the div in my html, where the information get sent to.

tadman
  • 208,517
  • 23
  • 234
  • 262
Fourty-Two
  • 77
  • 1
  • 6
  • Export it from MySQL in the correct format using `DATE_FORMAT` and `FROM_UNIXTIME` – Martin Oct 01 '18 at 19:25
  • 1
    Possible duplicate of [Convert timestamp to date in MySQL query](https://stackoverflow.com/questions/9251561/convert-timestamp-to-date-in-mysql-query) – Martin Oct 01 '18 at 19:26
  • i said in the question, that the user do have a option, how the date gets displayed, if the date get saved as: "month/day/year", then i can't display it as "day.month.year" anymore or do you know whats to do then? – Fourty-Two Oct 01 '18 at 19:27
  • 1
    When using an RDBMS like MySQL it's usually better to save data in the native format, which for MySQL is a `DATETIME` column. – tadman Oct 01 '18 at 19:31
  • ok, if the date get saved as "datetime" like: 2018-10-13 00:00:01, how can i output the date in the post as like: 10/13/2018 or like 13.10.2018? – Fourty-Two Oct 01 '18 at 19:39
  • @Fourty-Two [like this](https://stackoverflow.com/questions/52597575/how-to-transform-the-timestamp-in-seconds-stored-in-database-to-a-date-via-a?noredirect=1#comment92129834_52597575) – Martin Oct 01 '18 at 19:53

0 Answers0