How to change the date format in PHP within a foreach loop using a prepared statement

OK, so I was looking around the Internet trying to find out how to format the date that has been pulled from a mysql database and did not get very far, so I thought I would write this blog post. I have been using Prepared Statements and PDO for a few years now as mysqli can be subject to mysql injection. I was pulling a ‘timestamp’ data type from a mysql database which contains the date and time that the record was created.Lets go through the code step by step.

A Prepared Statement gets the data

Next we need to use the data in a table

So what does it look like when you finished?

Below is a screenshot of my little application. The date can be manipulated to remove the year or to display the time if you wish.

I have been using Prepared Statements and PDO for a few years now as mysqli can be subject to mysql injection. I was pulling a 'timestamp' data type from a mysql database which contains the date and time that the record was created

PHP Function reference

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.strtotime.php

Leave a Reply

Your email address will not be published.