PDA

Click to See Complete Forum and Search --> : [RESOLVED] Converting Date Types


Slyke
Oct 16th, 2008, 08:00 AM
Is there a function in PHP that will allow me to convert from one date/time format to another?

For example:

echo convdate("originalformat", "originaldatetime", "newformat");

I wish to convert dates and times from this:
16/10/2008 23:57:21

To something like this:
20081016235721

I've checked Google, but not much help from there.

I_Love_My_Vans
Oct 16th, 2008, 08:49 AM
Try this, I had written this because I couldn't find a way of converting the format of a date.

usage:
echo(format_date('16/10/2008 23:57:21'));

code:
function format_date($timestamp_date='', $result_format='YmdHis')
{
if($timestamp_date=='')
{
// Return if no variables passed
return;
}
else
{
// Split up date and time
$date_time = split(" ", $timestamp_date);

// Split up date
$date = split("/", $date_time[0]);

// Split up time
$time = split("-", $date_time[1]);

// Compile date into desired format
$result=date($result_format, mktime($time[0], $time[1], $time[2], $date[1], $date[0], $date[2]));
}

// Return resulting variable to main function
return $result;
}

Edit: I know this isn't very versatile now... I plan on making this function more efficient soon.

ILMV

Slyke
Oct 16th, 2008, 09:30 PM
Hey,

Thanks for that.

I'm sure that I can work for this =). Maybe when you finish your code you could put it on the PHP site :). Will marked resolved when I've got home and tested it. At work now!

I_Love_My_Vans
Oct 17th, 2008, 01:13 AM
My Pleasure

ILMV

Slyke
Oct 17th, 2008, 02:57 AM
Awesome dude, it worked like a charm :).

Thanks for your help!