Convert DateTime to String PHP

后端 未结 6 1923
醉梦人生
醉梦人生 2020-11-30 18:27

I have already researched a lot of site on how can I convert PHP DateTime object to String. I always see \"String to DateTime\" and not \"DateTime to String\"

PHP D

6条回答
  •  萌比男神i
    2020-11-30 18:54

    You can use the format method of the DateTime class:

    $date = new DateTime('2000-01-01');
    $result = $date->format('Y-m-d H:i:s');
    

    If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:

    if ($result) {
      echo $result;
    } else { // format failed
      echo "Unknown Time";
    }
    

提交回复
热议问题