Converting string to Date and DateTime

后端 未结 10 1710
伪装坚强ぢ
伪装坚强ぢ 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:29

    $d = new DateTime('10-16-2003');
    
    $timestamp = $d->getTimestamp(); // Unix timestamp
    $formatted_date = $d->format('Y-m-d'); // 2003-10-16
    

    Edit: you can also pass a DateTimeZone to DateTime() constructor to ensure the creation of the date for the desired time zone, not the server default one.

提交回复
热议问题