How to convert MySQL time to UNIX timestamp using PHP?

前端 未结 6 407
情歌与酒
情歌与酒 2020-12-02 15:09

There are a lot of questions that ask about \'UNIX timestamp to MySQL time\'. I needed the reversed way, yea... Any idea?

6条回答
  •  死守一世寂寞
    2020-12-02 15:38

    From one of my other posts, getting a unixtimestamp:

    $unixTimestamp = time();
    

    Converting to mysql datetime format:

    $mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
    

    Getting some mysql timestamp:

    $mysqlTimestamp = '2013-01-10 12:13:37';
    

    Converting it to a unixtimestamp:

    $unixTimestamp = strtotime('2010-05-17 19:13:37');
    

    ...comparing it with one or a range of times, to see if the user entered a realistic time:

    if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
    {...}
    

    Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:

    if(ctype_digit($_GET["UpdateTimestamp"]))
    {...}
    

提交回复
热议问题