admin管理员组

文章数量:1403435

I have the following date format in a MySQL database and was wondering what would be the best method to reformat this date and output what I have in mind.

MySQL: 2012-04-12

Desired Output: Thursday, April 12 2012

Perhaps I could format it in the MySQL query? Or with Java script?

Could anyone provide a sample of both so I could learn?

I have the following date format in a MySQL database and was wondering what would be the best method to reformat this date and output what I have in mind.

MySQL: 2012-04-12

Desired Output: Thursday, April 12 2012

Perhaps I could format it in the MySQL query? Or with Java script?

Could anyone provide a sample of both so I could learn?

Share Improve this question edited May 18, 2014 at 9:13 elboletaire 5,3872 gold badges37 silver badges47 bronze badges asked Apr 12, 2012 at 14:24 suchislifesuchislife 1
Add a ment  | 

7 Answers 7

Reset to default 3

Try moment.js

e.g. var day = moment("2012-04-12", "DDDD, MMMM DD YYYY");

You have a lot of ways to do this:

  • With MySQL, using DATE_FORMAT http://dev.mysql./doc/refman/5.5/en/date-and-time-functions.html#function_date-format
  • With PHP, using strtotime and date: date('J, M d Y', strtotime($yourdate))
  • With JavaScript, splitting the date at "-" and then creating your date again with new Date()
  • Also (like explained by t0s6i), with JavaScript, you can use the library momentjs to do something like this: date = moment("2012-04-12", "DDDD, MMMM DD YYYY");.

For formatting in javascript using jquery, loo at this other post.

Best JavaScript Date Parser & Formatter?

I would probably format with the code actually pulling the date form MySql. What language are you using? php, asp, java...?

PHP has a convenient class for reading dates in a specific format:

http://www.php/manual/en/datetime.createfromformat.php

$result = mysql_query("select date_column from table_name limit 1");
$array = mysql_fetch_array($result);

print DateTime::createFromFormat('Y-m-d')->format('jS M Y');

The best approach is usually to format the value with the PHP side just before outputting it:

$date = DateTime::createFromFormat('Y-m-d', $row['the_date']);
echo $date->format('J, M d Y');

When you have an date with mysql, it's you receive it in Php no?

for optimize your performance, use the native php date function to convert your mysql date , it's easy:

read just the official PHP doc => http://php/manual/fr/function.date.php

Php treatments as more fast than javascript treatments.

From javascript you can format your date using below function, DEMO

Note: The below function additionally displays sup for date (ex: 3rd, 12th e.t.c). Remove all the if..else if in below code if you don't want it.

function formatDate(d) {

    var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var curr_day = d.getDay();
    var curr_date = d.getDate();
    var sup = "";
    if (curr_date == 1 || curr_date == 21 || curr_date == 31) {
        sup = "st";
    } else if (curr_date == 2 || curr_date == 22) {
        sup = "nd";
    } else if (curr_date == 3 || curr_date == 23) {
        sup = "rd";
    } else {
        sup = "th";
    }

    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();

    return  d_names[curr_day] + " " + m_names[curr_month] + " " + curr_date + sup + "  " + curr_year;

}

Reference: Javascript Date formatting (above is modified version of Format #5)

本文标签: javascriptJQuery format date from MySQL HowStack Overflow