I'm having some issues with the next problem. I have a function to get all the dates from a SQL table. And I would like to change those dates format to a dd/mm/YY style. I think I should do it through a __construct function, but I'm not able to solve the problem.
This is my current function, which gives me all the dates on the SQL format, YY-mm-dd.
public function get_post($id = null) {
$q = 'SELECT id, date_published FROM posts';
if ($id) {
$q.=' WHERE id = :id';
}
$stmt = $this->con->prepare($q);
if ($id) {
$stmt->bindParam(':id', $id);
}
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, 'post');
return $stmt;}
I've tried including a function inside this same function with the __construct, but I don't know how to format the date.
I tried with:
this->date_published = // new formatted date
$date_published = // new formatted date
but those don't work.
Any help will be much appreciated. Thanks!
Use getters and setters (PHP magic methods):
public function __get($property, $value) {
if ($property === "date_published") {
$this->$property = // new formatted value
}
return $this;
}