How to convert date to timestamp in PHP?

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

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

相关标签:
19条回答
  • 2020-11-22 06:27

    If you're looking to convert a UTC datetime (2016-02-14T12:24:48.321Z) to timestamp, here's how you'd do it:

    function UTCToTimestamp($utc_datetime_str)
    {
        preg_match_all('/(.+?)T(.+?)\.(.*?)Z/i', $utc_datetime_str, $matches_arr);
        $datetime_str = $matches_arr[1][0]." ".$matches_arr[2][0];
    
        return strtotime($datetime_str);
    }
    
    $my_utc_datetime_str = '2016-02-14T12:24:48.321Z';
    $my_timestamp_str = UTCToTimestamp($my_utc_datetime_str);
    
    0 讨论(0)
  • 2020-11-22 06:28

    Using strtotime() function you can easily convert date to timestamp

    <?php
    // set default timezone
    date_default_timezone_set('America/Los_Angeles');
    
    //define date and time
    $date = date("d M Y H:i:s");
    
    // output
    echo strtotime($date);
    ?> 
    

    More info: http://php.net/manual/en/function.strtotime.php

    Online conversion tool: http://freeonlinetools24.com/

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

    Using mktime:

    list($day, $month, $year) = explode('-', '22-09-2008');
    echo mktime(0, 0, 0, $month, $day, $year);
    
    0 讨论(0)
  • 2020-11-22 06:32
    function date_to_stamp( $date, $slash_time = true, $timezone = 'Europe/London', $expression = "#^\d{2}([^\d]*)\d{2}([^\d]*)\d{4}$#is" ) {
        $return = false;
        $_timezone = date_default_timezone_get();
        date_default_timezone_set( $timezone );
        if( preg_match( $expression, $date, $matches ) )
            $return = date( "Y-m-d " . ( $slash_time ? '00:00:00' : "h:i:s" ), strtotime( str_replace( array($matches[1], $matches[2]), '-', $date ) . ' ' . date("h:i:s") ) );
        date_default_timezone_set( $_timezone );
        return $return;
    }
    
    // expression may need changing in relation to timezone
    echo date_to_stamp('19/03/1986', false) . '<br />';
    echo date_to_stamp('19**03**1986', false) . '<br />';
    echo date_to_stamp('19.03.1986') . '<br />';
    echo date_to_stamp('19.03.1986', false, 'Asia/Aden') . '<br />';
    echo date('Y-m-d h:i:s') . '<br />';
    
    //1986-03-19 02:37:30
    //1986-03-19 02:37:30
    //1986-03-19 00:00:00
    //1986-03-19 05:37:30
    //2012-02-12 02:37:30
    
    0 讨论(0)
  • 2020-11-22 06:34

    Be careful with functions like strtotime() that try to "guess" what you mean (it doesn't guess of course, the rules are here).

    Indeed 22-09-2008 will be parsed as 22 September 2008, as it is the only reasonable thing.

    How will 08-09-2008 be parsed? Probably 09 August 2008.

    What about 2008-09-50? Some versions of PHP parse this as 20 October 2008.

    So, if you are sure your input is in DD-MM-YYYY format, it's better to use the solution offered by @Armin Ronacher.

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


    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)
提交回复
热议问题