**Disclaimer:
My blog contains content that may be objectionable. Some of the posts in this blog may contain strong language or (because of my religious and personal views) may be offensive to some readers. In the future I may separate my programming posts from personal posts, but for now they are all contained in this blog. I should not have to even mention this, however some readers may be looking at my blog for programming help, therefore I am giving an advanced warning. All posts with material that may be offensive contain 'Objectionable material' at the end of the post title. If you see a post with "Objectionable material" in the title, and you are closed-minded or think that you may get queasy, don't fucking read it.


Sunday, August 8, 2010

Get Date from MySQL Timestamp in PHP


Sometimes you need to do something with a Timestamp taken from MySQL. Whether that is formatting the Timestamp for better user readability, or to do some sort of processing with it, this can be easily accomplished by converting the Timestamp to a Date. The format of a Timestamp in MySQL is:
'YYYY-MM-DD HH:MM:SS'
We can convert to a Date simply by using PHP's strtotime function:
//Normally the Timestamp would come from MySQL, but
//for the example I will define it here.
$timestamp = '2010-08-08 15:25:18';
echo date('D M j G:i:s Y', strtotime($timestamp));
echo '<br>';
echo date('d-m-y', strtotime($timestamp));


This will output the following lines:
Sun Aug 8 15:25:18 2010
08-08-10

Read more about the date and strtotime functions.

No comments:

Post a Comment