Converting string to Date and DateTime

后端 未结 10 1705
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 17:07

If I have a PHP string in the format of mm-dd-YYYY (for example, 10-16-2003), how do I properly convert that to a Date and then a DateTime

10条回答
  •  渐次进展
    2020-11-22 17:32

    Like we have date "07/May/2018" and we need date "2018-05-07" as mysql compatible

    if (!empty($date)) {
        $timestamp = strtotime($date);
        if ($timestamp === FALSE) {
             $timestamp = strtotime(str_replace('/', '-', $date));
         }
             $date = date('Y-m-d', $timestamp);
      }
    

    It works for me. enjoy :)

提交回复
热议问题