Jump to content

Generate unixtimestamp based off of html form


weee

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.