Nerve
Nerve

Reputation: 33

convert integer date into formatted text

I have an sql database with a date column like this yyyymmdd (20111109), i usually sort my return queries by the date.

I would like to know how to convert this to the (D jS M) or (Wed 9th Nov).

For the month, date, and ordinal suffix, im using substr along with case. but its the weekday im getting frustrated with.

Is there a simpler method?

Thanks in advance.

Upvotes: 3

Views: 5129

Answers (3)

GEMI
GEMI

Reputation: 2279

If you are interested in something more sophisticated:

  $date = DateTime::createFromFormat('Ymd', '20111109');
  echo $date->format('D jS M');

This should be more reliable in the long run.

Upvotes: 2

Dastca
Dastca

Reputation: 81

Try using strtotime and the date() function

with a combination of those you can achieve:

echo date("D j, M", strtotime(20111109));

Upvotes: 1

Klemen Tusar
Klemen Tusar

Reputation: 9689

This should work:

date_default_timezone_set('America/Los_Angeles');
$date = strtotime($your_string);
$formatted_date = date('D jS M', $date);
echo $formatted_date;

Upvotes: 2

Related Questions