A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearmm/dd/yyyy format to epoch with PHP - page 1
User InfoPosts
mm/dd/yyyy format to epoch with PHP#1
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!

posted date: 2008-12-11 07:46:00


Re: mm/dd/yyyy format to epoch with PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 07:46:01


Re: mm/dd/yyyy format to epoch with PHP#3
You should look at the mktime() function. Couple that with either regular expressions or strtotime(), and you'll have it.

posted date: 2008-12-11 07:48:00


Re: mm/dd/yyyy format to epoch with PHP#4
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!

posted date: 2008-12-11 08:16:00


Re: mm/dd/yyyy format to epoch with PHP#5
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);

posted date: 2008-12-11 08:21:00


Re: mm/dd/yyyy format to epoch with PHP#6
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.

posted date: 2008-12-11 08:38:00


Re: mm/dd/yyyy format to epoch with PHP#7
Also, don't forget to validate input: strtotime('15/05/2008') is FALSE, not 1210802400.

posted date: 2008-12-11 08:50:00


Re: mm/dd/yyyy format to epoch with PHP#8
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.

posted date: 2008-12-11 09:01:00


Re: mm/dd/yyyy format to epoch with PHP#9
You would have to parse $epochhold into 3 variables and then pass all three to the mktime. You can't just pass one argument and expect PHP to split it into three! I'd suggest the split() function.

posted date: 2008-12-11 09:15:00


Re: mm/dd/yyyy format to epoch with PHP#10
strtotime is the great equalizer of dates in PHP

posted date: 2008-12-11 09:51:00


Re: mm/dd/yyyy format to epoch with PHP#11
unicoins!!!!!!!!!

posted date: 2014-04-01 12:39:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.