Saturday, May 24, 2008

Date Formatting in PHP, Change Date Format

you can easily change date format (mm/dd/yy) to (dd/mm/yy) ...

Example:

<?


formatdate(date('Y/m/d'));


function formatdate($mysql_stamp,$type=1)

{

//$type = 1 date + time

//$type = 2 date

//$type = 3 time

// split mysql DATETIME stamp into date and time

if($mysql_stamp==""){

return "";

}

@list($date, $time) = split ('[ ]', $mysql_stamp);

@list($year, $month, $day) = split ('[-]', $date);

if( isset($time) && $time != "" )

{

list($hour, $minute, $second) = split ('[:]', $time);

if($hour>=12)

{

$ext = "PM";

$hour = $hour - 12;

}

else

{

$ext = "AM";

}

$time = " ".$hour.":".$minute." ".$ext;

}

else

{

$hour="";

$minute= "" ;

$ext = "";

$time = "";

}



if($type == 1)

$formatted_stamp = "$day/$month/$year".$time;

elseif($type==2)

{

$formatted_stamp = "$day/$month/$year";

}

elseif($type==3)

$formatted_stamp = $time;


return $formatted_stamp;

}


?>

No comments: