2

MySQL database has date field which contents date format as 2016-02-28 02:11:24. Following php code and j-query displays date on bootstrap model. I want to change the date format as 28 Feb 2016, 02:11 PM How to do it using front end J-query or back end MySQL field format?

<?php
    foreach ($value as $value2) {
        echo "<div class='well'>";
        echo '<p class="target" userRegdate = "' . $value2->added_date . '"  >' . $value2->content . '</p>';
        echo "</div>";
    }
?>

J-query dialog box to display date

//Display dialog box when mouse click
$(".target").click(function () {

    var publish_date = $(this).attr("userRegdate");
    BootstrapDialog.show({
        title: 'user added date',
        message: publish_date            
    });
});
1
  • You could use the momentjs library: momentjs.com/docs Commented Mar 2, 2016 at 16:39

5 Answers 5

1
//code added the date function with parameter to format date
<?php
    foreach ($value as $value2) {
        echo "<div class='well'>";
        echo '<p class="target" userRegdate = "' . date("j, F, Y, g:i a",strtotime($value2->added_date)) . '"  >' . $value2->content . '</p>';
        echo "</div>";
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This code-only answer doesn't explain what was wrong or how to fix it.
1

If you want to use it using PHP then :

<?php
 $yrdata= strtotime('2016-02-28 02:11:24');
    echo date('d M,Y H:i A', $yrdata);
 ?>

OUTPUT

28 Feb,2016 02:11 AM

Using jquery :

You can use jQuery dateFormat plugin .

Comments

1

As you're saying you get the data from the database, you could simply format it from there so you don't need to do something on the front-end (or with php) at all. You can use MySQL's DATE_FORMAT() function:

SELECT DATE_FORMAT('2016-02-28 02:11:24', '%e %b %Y, %r')

in a query like

SELECT DATE_FORMAT(dateColumn, '%e %b %Y, %r') formatedDate, colA, colB 
FROM table

Comments

1

With moment.js

var date = new Date(*added date*);
var formattedDate = moment(date).format('dd MMM YYYY, h:mm');

where added date is 2016-02-28 02:11:24

Comments

0

Other form is this if you want change the sort easy:

var newDate = date.getDate() + '/' + (date.getMonth() + 1) + '/' +  date.getFullYear() + ', '+ date.getHours() + ':' + date.getMinutes() + 'PM';

https://jsfiddle.net/bxa5efzj/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.