weee Posted October 30, 2010 Share Posted October 30, 2010 Hello, I have ran into a unique scenario. I am trying to generate a unix timestamp (by using the gmmktime function). The problem is, I have multiple tags in a form doing the same thing. for example (I have truncated some of the values for sake of page length/readability): <label for="dateMonth[6]">Date:</label> Month: <select name="dateMonth[6]" id="dateMonth[6]"> <option value="9">September</option> <option value="10" selected="selected">October</option> <option value="11">November</option> <option value="12">December</option> </select> Day: <select name="dateDay[6]" id="dateDay[6]"> <option value="29">29</option> <option value="30" selected="selected">30</option> <option value="31">31</option> </select> Year: <select name="dateYear[6]" id="dateYear[6]"> <option value="2010" selected="selected">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> </select> Hour: <select name="dateHour[6]" id="dateHour"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> Minute: <select name="dateMinute[6]" id="dateMinute"> <option value="0" selected="selected">00</option> <option value="15">15</option> <option value="30">30</option> <option value="45">45</option> </select> AM/PM: <select name="AMPM[6]" id="AMPM"> <option value="AM">AM</option> <option value="PM" selected="selected">PM</option> </select> Now, as you can see, I have an array of these elements. AMPM[6] dateHour[6] dateMinute[6], AMPM[7] dateHour[7] dateMinute[7], AMPM[8] and so on... The problem I am having is coming with the logic to consolidate all of the post variables that are 6s to make the unix timestamp, then all of the 7s to make a timestamp, and so on. Could someone please point me in the right direction on how to go about processing this? Thank you! weee Quote Link to comment https://forums.phpfreaks.com/topic/217283-generate-unixtimestamp-based-off-of-html-form/ Share on other sites More sharing options...
joel24 Posted October 30, 2010 Share Posted October 30, 2010 strtotime() Quote Link to comment https://forums.phpfreaks.com/topic/217283-generate-unixtimestamp-based-off-of-html-form/#findComment-1128333 Share on other sites More sharing options...
weee Posted October 30, 2010 Author Share Posted October 30, 2010 strtotime doesn't really work. It wouldn't be as userfriendly for the user to type in month day year hour:minute AM in a textbox... Are there any other solutions that we could use? Quote Link to comment https://forums.phpfreaks.com/topic/217283-generate-unixtimestamp-based-off-of-html-form/#findComment-1128500 Share on other sites More sharing options...
DavidAM Posted October 30, 2010 Share Posted October 30, 2010 I think what joel24 was saying is to build up the date string and run it through strtotime() to get the unix time. Something like this: for ($ind = 1; $ind <= 8; $ind++) { $dateStr = sprintf('%d/%d/%d %d:%d %s', $_POST['dateMonth'][$ind], $_POST['dateDay'][$ind], $_POST['dateYear'][$ind], $_POST['dateHour'][$ind], $_POST['dateMinute'][$ind], $_POST['AMPM'][$ind]); $userTime[$ind] = strtotime($dateStr); } print_r($userTime); I like sprintf(). You could choose to use concatenation (did I spell that right?) or even mktime() (with special handling for the HOUR and AMPM values). There are probably other ways as well. * This code is: not tested; does not sanitize the input; and assumes all POSTed values are valid. The user is responsible for cleaning it up as necessary. Quote Link to comment https://forums.phpfreaks.com/topic/217283-generate-unixtimestamp-based-off-of-html-form/#findComment-1128504 Share on other sites More sharing options...
weee Posted October 31, 2010 Author Share Posted October 31, 2010 Thank you david! That thought process helped me work this out Quote Link to comment https://forums.phpfreaks.com/topic/217283-generate-unixtimestamp-based-off-of-html-form/#findComment-1128553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.