1

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?

elboletaire
  • 5,180
  • 2
  • 36
  • 46
suchislife
  • 4,251
  • 10
  • 47
  • 78

7 Answers7

3

Try moment.js

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

t0s6i
  • 161
  • 3
2

You have a lot of ways to do this:

elboletaire
  • 5,180
  • 2
  • 36
  • 46
0

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

Community
  • 1
  • 1
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42
0

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

http://www.php.net/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');
steveukx
  • 4,370
  • 19
  • 27
0

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');
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
0

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.net/manual/fr/function.date.php

Php treatments as more fast than javascript treatments.

Doc Roms
  • 3,288
  • 1
  • 20
  • 37
0

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)

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134