How to convert date to timestamp in PHP?

前端 未结 19 2346
暗喜
暗喜 2020-11-22 05:38

How do I get timestamp from e.g. 22-09-2008?

19条回答
  •  不要未来只要你来
    2020-11-22 06:23

    If you want to know for sure whether a date gets parsed into something you expect, you can use DateTime::createFromFormat():

    $d = DateTime::createFromFormat('d-m-Y', '22-09-2008');
    if ($d === false) {
        die("Woah, that date doesn't look right!");
    }
    echo $d->format('Y-m-d'), PHP_EOL;
    // prints 2008-09-22
    

    It's obvious in this case, but e.g. 03-04-2008 could be 3rd of April or 4th of March depending on where you come from :)

提交回复
热议问题