Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert integer date into formatted text

Tags:

php

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.

like image 272
Nerve Avatar asked Apr 01 '26 02:04

Nerve


2 Answers

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.

like image 100
GEMI Avatar answered Apr 02 '26 20:04

GEMI


This should work:

date_default_timezone_set('America/Los_Angeles');
$date = strtotime($your_string);
$formatted_date = date('D jS M', $date);
echo $formatted_date;
like image 42
Klemen Tusar Avatar answered Apr 02 '26 20:04

Klemen Tusar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!