mm/dd/yyyy format to epoch with PHP

与世无争的帅哥 提交于 2019-11-29 19:10:50

问题


I have a mysql table that relies on the unix epoch time stamp equivalent of the date of the entry to sort and filter in various parts of the website. I'm trying to implement a date picker that will enter the date into the form field in the mm/dd/yyyy format. I've been struggling with converting that date into the unix epoch format to add that entry in the row field. All the attempts I've made have resulted in generating the current day epoch time stamp. Does anyone have any idea how I can take that date format and convert in the it's equivalent epoch time stamp?

Thanks in advance.

Additional information:

I have been trying mktime, and all I get is todays epoch. Apologies, I should have added some code earlier to better explain:

The form id is "date" The database field is "epoch"

Here's what I'm trying (unsuccessfully) when I post the for date:

$epochhold = ($_POST['date']);
$epoch = mktime(0,0,0,$epochhold);

I understand from a previous post that this would still submit the value as mm/dd/yyyy and not mm, dd, yyyy as mktime expects, however the post didn't offer and resolution on how to do that. I tried a str_replace to change the "/" to "," and that yeilded the same result - getting today's epoch date regardless of the date entered.

Here's that code example - again this didn't work, but I add it to help illustrate what I've tried

$epochhold = ($_POST['date']);
$epochold2 = str_replace('/', ', ', $epochhold)
$epoch = mktime(0,0,0,$epochhold2);

Thanks for the previous response as well, I didn't want the quick reply to go unnoticed!

Thanks everyone!

Thanks to everyone for the replies - strtotime seems to be working best on the initial tests and may be the way to go as this format is consistent throughout the site. But all the suggestions helped with this a few other things all well so thank you all again!


回答1:


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!




回答2:


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




回答3:


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);



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/359782/mm-dd-yyyy-format-to-epoch-with-php

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