strtotime for DD/MM/YYYY not working properly [duplicate]

Deadly 提交于 2021-02-08 10:00:11

问题


I got this type of date from DB '10/09/19' It's today date(10th sept 2019).

$date = '10/09/19';
       //DD/MM/YY
$newdate=date('Y-m-d',strtotime(str_replace('/', '-', $date)));
echo $newdate; //output 2010-09-19
exit;

It's giving me 2010-09-19. it means 19th Sept 2010.

I expect 10-09-2019(10th Sept 2019). I already tried many solutions from SO but no luck. Can anyone tell me what am I doing wrong??


回答1:


The DateTime class can be used in situations like this - it has the useful method createFromFormat

    $date='10/09/19';   //dd/mm/yy
    $new=DateTime::createFromFormat('d/m/y', $date )->format('Y/m/d');
    echo $new;  //2019/09/10


来源:https://stackoverflow.com/questions/57871282/strtotime-for-dd-mm-yyyy-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!