mm/dd/yyyy format to epoch with PHP

那年仲夏 提交于 2019-11-30 13:46:50

If you know that it will always be in that format, strtotime will convert it directly into the unix timestamp.

strtotime($_POST['app_date']);

HTH!

Vilx-

You should look at the mktime() function. Couple that with either regular expressions or strtotime(), and you'll have it.

if ( ! preg_match('#\d{2}/\d{2}/\d{4}#', $_POST['date']) ) {
    // complain about invalid input
}

list($m, $d, $y) = explode('/', $_POST['date']);
$timestamp = mktime(0, 0, 0, $m, $d, $y);

You're gonna need to send the dd,mm,yyyy as seperate vars to mktime. It takes the value in $epochold2 as one string that would be invalid for mktime.

Your best bet would be to use the strtotime as I previously stated, or to follow Ant P's advice using mktime.

strtotime() will parse just about any date format and return the Unix timestamp. All you have to do is pass it your date/time string:

$unix_time = strtotime($_POST['date']);
if($unix_time < 0) {
    //error case
}
else {
    //value OK!
}

As you can see above, you can even use it to help validate input - if a user enters a date like 02/31/2008, it will return -1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!