PHP date format converting

后端 未结 4 1564
青春惊慌失措
青春惊慌失措 2020-11-29 13:00

Here is what I have:

$dateFormat = \'M d, Y\';
$dateString = \'January 23, 2010\';

What I need is a timestamp of $dateString s

4条回答
  •  没有蜡笔的小新
    2020-11-29 13:16

    As of PHP5.3 you can use the DateTime API

    $dt = date_create_from_format('M d, Y', 'January 23, 2010');
    echo date_timestamp_get($dt);
    

    or with OOP notation

    $dt = DateTime::createFromFormat('M d, Y', 'January 23, 2010');
    echo $dt->getTimestamp();
    

    Note that while DateTime is available in PHP < 5.3, the methods used above are not and while you could simulate ->getTimestamp() with ->format('U'), there is no easy workaround for createFromFormat()

    An alternative would be to use Zend_Date from Zend Framework:

    $date = new Zend_Date('Feb 31, 2007', 'MM.dd.yyyy');
    echo $date->get(Zend_Date::TIMESTAMP);
    

提交回复
热议问题