Forum Moderators: coopster

Message Too Old, No Replies

Separating dates

         

Esqulax

3:35 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



I have a date in my SQL database, but i want to separate it into its different components.
its stored in my database like.. y-m-d
in a field called fulldate.

Any ideas would be really appreciated

too much information

3:40 pm on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's pretty simple, just use "explode" to break apart the string.

$date = explode("-",mySQLdate);
$day = $date[0];
$month = $date[1];
$year = $date[2];

So basically the explode command needs two things. The thing that separates the parts of the string and the string to break apart. That is stored in an array that I called $date.

Then each part of the date can be assigned to other variables or you can use them as $date[0], $date[1], etc.

Tommybs

3:40 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Hi,

You could create a function that seperates it like so

function formatDate($date){
$day = substr($date,8,2);
$mon = date('M', mktime(1,1,1,substr($date,5,2),1,date('Y')));
$year = substr($date,0,4);
$date = "$day $mon $year";
return $date;
}

Tommybs

3:46 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Should probably mention this will return the month as a 3 letter representation e.g Mar, Feb and the year will be 4 characters long.

Oh and just realised I got here after someone else as well!

Esqulax

4:03 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



That works great!
Is there a way to format the date when it gets taken from the database? at the mo its:
2008-03-04
y m d
id rather it be 08-03-04