0

I'm not very experienced in ajax and having trouble formatting my date from a MySQL database.

I've tried using the php date() function but can't seem to get it to work properly. What would be the best way to format the date in this?

  $(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "../api/presentation/read.php",
        dataType: 'json',
        success: function(data) {
            var response="";
            for(var user in data){
                response += "<tr>"+
                "<td>"+data[user].date+"</td>"+
                "<td>"+data[user].title+"</td>"+
                "<td>"+data[user].speaker+"</td>"+
                "<td><a href='update.php?id="+data[user].id+"'>Edit</a> | <a href='#' onClick=Remove('"+data[user].id+"')>Remove</a> | <a href='#' onClick=Active('"+data[user].id+"')>Active</a></td>"+
                "</tr>";
            }
            $(response).appendTo($("#presentations"));
        }
    });
  });

I have tried something along the lines of

"<td><?php echo date("+data[user].date+",dd-mm); ?></td>"+

This gives an output of "+01am31am[00000000UTCThu, 01 Jan 1970 00:00:00 +0000].01am31UTC+" which I suspect is because no value is being given to the date function?

I'd like the output to be in the format of dd-mm HH-MM instead of the MySQL datetime format of "2019-10-29 10:00:00"

3
  • Welcome to SO, Jordan. Can you tell us what happens when you use the date function and what the desired result should be? Please add it to your question above. Commented Oct 27, 2019 at 18:00
  • Currently outputs "2019-10-29 10:00:00". Commented Oct 27, 2019 at 18:13
  • "I have tried something along the lines of "<td><?php echo date("+data[user].date+",dd-mm); ?></td>"+" - What do you mean exactly? That appears to be missing something as to where and how it's being used. You are already inside double quotes and using the same inside that, then you have + characters. Do you want to add or use as a concatenate method? Commented Oct 27, 2019 at 18:30

2 Answers 2

1

You are mixing PHP and JavaScript in a wrong way.

First, you MUST know that PHP is executed on server side, BEFORE any HTML or JavaScript exists on your browser.

That said, lets read your attempt again:

""

Here, the PHP part of script will be run on server side, and the data[...] Variable don't exists there, also it's inside a string (between double quotes). Since this string is not a valid date, it will output the start of unix epoch.

To solve this, you need to separate your concerns here.

First way

  1. Your php script called by ajax should return the date in some valid date format. I suggest using ISO8601 format since JavaScript parses this format automatically when using Date object.
  2. Use JavaScript to format that date in the desired output format.

Second way:

  1. Directly convert the date on PHP using the date function, but: you do it entirely on the PHP script. Don't mix both languages. They are totally separated and runs in different moment's and have isolated scopes.
Sign up to request clarification or add additional context in comments.

Comments

0

Make the change in your Select query and then do as you have now.

SELECT DATE_FORMAT(NOW(), '%d.%m.%Y %H:%i:%s') 

Result

DATE_FORMAT(NOW(), '%d.%m.%Y %H:%i:%s')
27.10.2019 20:40:41

instead of the function now() put in your columnname

Comments

Your Answer

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