PHP Function to Return Readable mySQL Timestamps

Something almost every PHP app needs is a way to take Mysql timestamps and convert them to a readable format. So, I went and wrote a few functions to handle this. I used PHP’s date function and strtotime to make this happen. You can edit what it returns by understanding how PHP’s date function works.

Here is the function:

function stampfixer($data)
if($data=='0000-00-00 00:00:00' or $data=='')
return '';
$cdate = strtotime($data);
return date("m-d-Y h:i A",$cdate);

This function will take a timestamp you throw at it and turn it into something like this:

01-01-1980 12:00 AM

You can change it to not show the AM/PM, 24 hour format instead of 12, just about anything by editing:

return date("m-d-Y h:i A",$cdate)

See PHP date function for what variables to throw at it to get it to do what you want.

Post a Comment