How to convert date to timestamp in PHP?

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

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

相关标签:
19条回答
  • 2020-11-22 06:11
    <?php echo date('U') ?>
    

    If you want, put it in a MySQL input type timestamp. The above works very well (only in PHP 5 or later):

    <?php $timestamp_for_mysql = date('c') ?>
    
    0 讨论(0)
  • 2020-11-22 06:13

    With DateTime API:

    $dateTime = new DateTime('2008-09-22'); 
    echo $dateTime->format('U'); 
    
    // or 
    
    $date = new DateTime('2008-09-22');
    echo $date->getTimestamp();
    

    The same with the procedural API:

    $date = date_create('2008-09-22');
    echo date_format($date, 'U');
    
    // or
    
    $date = date_create('2008-09-22');
    echo date_timestamp_get($date);
    

    If the above fails because you are using a unsupported format, you can use

    $date = DateTime::createFromFormat('!d-m-Y', '22-09-2008');
    echo $dateTime->format('U'); 
    
    // or
    
    $date = date_parse_from_format('!d-m-Y', '22-09-2008');
    echo date_format($date, 'U');
    

    Note that if you do not set the !, the time portion will be set to current time, which is different from the first four which will use midnight when you omit the time.

    Yet another alternative is to use the IntlDateFormatter API:

    $formatter = new IntlDateFormatter(
        'en_US',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'GMT',
        IntlDateFormatter::GREGORIAN,
        'dd-MM-yyyy'
    );
    echo $formatter->parse('22-09-2008');
    

    Unless you are working with localized date strings, the easier choice is likely DateTime.

    0 讨论(0)
  • 2020-11-22 06:14

    There is also strptime() which expects exactly one format:

    $a = strptime('22-09-2008', '%d-%m-%Y');
    $timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
    
    0 讨论(0)
  • 2020-11-22 06:14
    <?php echo date('M j Y g:i A', strtotime('2013-11-15 13:01:02')); ?>
    

    http://php.net/manual/en/function.date.php

    0 讨论(0)
  • 2020-11-22 06:20

    Given that the function strptime() does not work for Windows and strtotime() can return unexpected results, I recommend using date_parse_from_format():

    $date = date_parse_from_format('d-m-Y', '22-09-2008');
    $timestamp = mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
    
    0 讨论(0)
  • 2020-11-22 06:21


    This method works on both Windows and Unix and is time-zone aware, which is probably what you want if you work with dates.

    If you don't care about timezone, or want to use the time zone your server uses:

    $d = DateTime::createFromFormat('d-m-Y H:i:s', '22-09-2008 00:00:00');
    if ($d === false) {
        die("Incorrect date string");
    } else {
        echo $d->getTimestamp();
    }
    

    1222093324 (This will differ depending on your server time zone...)


    If you want to specify in which time zone, here EST. (Same as New York.)

    $d = DateTime::createFromFormat(
        'd-m-Y H:i:s',
        '22-09-2008 00:00:00',
        new DateTimeZone('EST')
    );
    
    if ($d === false) {
        die("Incorrect date string");
    } else {
        echo $d->getTimestamp();
    }
    

    1222093305


    Or if you want to use UTC. (Same as "GMT".)

    $d = DateTime::createFromFormat(
        'd-m-Y H:i:s',
        '22-09-2008 00:00:00',
        new DateTimeZone('UTC')
    );
    
    if ($d === false) {
        die("Incorrect date string");
    } else {
        echo $d->getTimestamp();
    }
    

    1222093289


    Regardless, it's always a good starting point to be strict when parsing strings into structured data. It can save awkward debugging in the future. Therefore I recommend to always specify date format.

    0 讨论(0)
提交回复
热议问题