I have been trying to format a date that I receive from a value in a mysql database. The format in mysql is as follows.
1986-08-02
I want to convert that to
Aug. 2, 1986
I tried the date function in php and it was not working for me. Anyone, have any ideas how I can do this.
Thanks,
Alex
Is it possable to change the way in wich you add the dates to the database as keeping them in UNIX timestamps makes the whole date thing much much easier and better yet allows you to do more to it.
Ok if this isn't possable then you want to look into the explode function and the case statements as this will make up most of the function you need to make.
This should put you on the right track.
<?php
$date = 1986-08-02;
//We need to seperate the different parts YYYY-MM-DD
//We use the explode method for this we need to eplode the - chars
//So we need to create a variable holding our explode function
$parts = explode( "-" , $date );
//Now we should have the following
echo $parts[0] // 1986
echo $parts[1] // 08
echo $parts[2] // 02
// Now we create a case statement for each of the months!
switch( $parts[1] ) {
case 01:
$parts[1] = "Jan";
break;
case 02:
$parts[1] = "Feb";
break;
case 03:
$parts[1] = "Mar";
break;
case 04:
$parts[1] = "Apr";
break;
case 05:
$parts[1] = "May";
break;
case 06:
$parts[1] = "Jun";
break;
case 07:
$parts[1] = "Jul";
break;
case 08:
$parts[1] = "Aug";
break;
case 09:
$parts[1] = "Sep";
break;
case 10:
$parts[1] = "Oct";
break;
case 11:
$parts[1] = "Nov";
break;
case 12:
$parts[1] = "Dec";
break;
}
//Ok so now our $parts[1] (the month) is in the text format we can peice it back together
$newDate = $parts[1].". ".$parts[2].", ".$parts[0];
?>
Ok thats from the top of my head so it may need a little playing with, but it should be "Aug. 02, 1986". The only thing you may want to do is search the string for a 0 and if one exists replace it with "false".
To do this just add this line inder the name convertions for the months:
$parts[2] = str_replace( 0 , false , $parts[2] );
This should work you will have to test it out because as I said it is off the top of my head.