dougao2830 2017-10-19 18:42
浏览 29
已采纳

使用构造在类中格式化日期

im 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 Im not able to solve the problem.

This is my current function, wich 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 dont know how to format the date. I tried with:

this->date_published = // new formatted date

$date_published = // new formatted date

, but doesnt work.

Any help will be much appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • dongshi2141 2017-10-19 18:55
    关注

    Use getters and setters (PHP magic methods):

      public function __get($property, $value) {
        if ($property === "date_published") {
          $this->$property = // new formatted value
        }
    
        return $this;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号