...

View Full Version : Formatting a date



tomharto
03-21-2012, 12:44 PM
I have a string containing a date in the format YYYY-MM-DD and i want to format it so its something like 21st March 2012, how would i do this?

abduraooft
03-21-2012, 01:05 PM
echo date("dS F Y", strtotime($date)); // where $date contains the date in YYYY-MM-DD format
sorry, didn't notice the forum selected

Philip M
03-21-2012, 01:18 PM
Here you are (suggest you ignore the unhelpful posts - webdev1958/bullant cannot resist making sneering comments, rather than offering any help):-


<script type = "text/javascript">

var mths = ['January','February','March','April','May','June','July','August','September','October','November',' December'];

var str = "2012-03-21"; // the date you specified YYYY-MM-DD
var sp = str.split("-");
var yr = sp[0];
var mth = Number(sp[1])-1; // months in javascript are 0-11
var mthname = mths[mth];
var day = Number(sp[2]);
day = day + ["th","st","nd","rd"][!(day%10>3||Math.floor(day%100/10)==1)*day%10];
var finaldate = day + " " + mthname + " " + yr;
alert (finaldate);

</script>


Obviously one having captured the component parts of the date you can manipulate their order to display them in any format you wish. You could also easily add the name of the weekday if you wished.


"A good reputation can take years to aquire, a bad one takes a few seconds"

tomharto
03-21-2012, 01:22 PM
Here you are (suggest you ignore the unhelpful posts):-


<script type = "text/javascript">

var mths = ['January','February','March','April','May','June','July','August','September','October','November',' December'];

var str = "2012-03-21"; // the date you specified
var sp = str.split("-");
var yr = sp[0];
var mth = Number(sp[1])-1; // months in javascript are 0-11
var mthname = mths[mth];
var day = Number(sp[2]);
day = day + ["th","st","nd","rd"][!(day%10>3||Math.floor(day%100/10)==1)*day%10];
var finaldate = day + " " + mthname + " " + yr;
alert (finaldate);

</script>


Obviously one having captured the component parts of the date you can manuipulate their order to display them in any format you wish. You could also easily add the name of the weekday if you wished.


"A good reputation can take years to aquire, a bad one takes a few seconds"


Thank you, ill take a look at that soon as i get home :)

EDIT: Tested it works like a charm :) thanks a lot



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum