views:

2371

answers:

5

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  A: 

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

Vilx-
+2  A: 

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!

Matt
Also, don't forget to validate input: strtotime('15/05/2008') is `FALSE`, not `1210802400`.
Piskvor
strtotime is the great equalizer of dates in PHP
TravisO
+1  A: 
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);
Ant P.
A: 

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.

Matt
A: 

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.

Wickethewok