Jump to content

how to separate date in 3 variable?


angello

Recommended Posts

hi, i have a form that get?s date from a datepicker like this 12/05/2012 in 1 variable, but i want it after get the date from the datepicker, to put date it in 3 diferent variables:

$day

$month

$year

How can i do this?

code example:

<form action="destiniaXMLrequest.php" method="post" name="getxmlrequest">
     
        <label>Check In</label> <input type="date"  name="CheckIn" id="checkinflight"/><br/>
                             
        <label>Check Out</label><input  type="date"   name="CheckOut"  id="checkoutflight"/><br/>
               
       <input type="submit" value="Send" />
       <input type="reset" value="Clear Fields" />
   
    </form>

this is the php code:

$checkin_day  = $_POST['CheckIn'];
$checkout_day = $_POST['CheckOut'];
echo "this is the data you enter in the form:<br>";
echo $checkin_day."<br>";
echo $checkout_day."<br>";

echo("What i want to do is to put the date of the datepicker (in 1 variable) in 3 diferent variables<br>");
echo("like this:<br>");

echo ('$checkin_day'."<br>");
echo ('$checkin_month'."<br>");
echo ('$checkin_year'."<br>");

Thanks for any help guys

Link to comment
Share on other sites

The real question is why do you want it in three separate variables? What's your end goal with doing it like that?

 

To answer your question, check out explode () and list (). Though, I'm almost certain you're doing it, whatever it is you're doing, in an overly complicated manner.

Link to comment
Share on other sites

thanks for your answer cristianF, the thing is that im building a travel simple webpage, and i have a date form where you select the date from a datepicker as i explain in my post, but to see the results of hotels/flights (im in an traveling affiliate program) they ask me to interrogate or to ask their server  for this search in 3 diferen variables, so you would pick the date automatic from a datepicker in my web page  like this: 15/05/2012,with only 1 click,  but i have to send the xml request to their server like this:

find me a flight to this date:

$day

$month

$year

 

See now why im asking to solve this?

ill try what you just told me, but if with this explanation you thing there is an easier way please let me know, thanks again

Link to comment
Share on other sites

I completely agreed with ChristianF's original response. But, if you are required to send the data in three separate variables, then that is what you must do. But, if you are storing this data in the database you should absolutely be storing it as a valid date value and not separate values. Only separate into the components at the time that you need to.

 

ChristianF's solution is the one I would go with if you are taking the submitted value directly and parsing it out.

list($month, $day, $year) = explode('\', $_POST['CheckIn']);

Link to comment
Share on other sites

thank both of you guys, it works just great with explode, let me show you what im doing, i bought a template and i want to have a travel site with an affiliate program, and i dont know too much php as you can see but im learning, you can see the template http://www.vacacionesytours.com/

please go to the left and go to the "Prepare for traveling Form", then click on the field to choose the date (date picker was not part of the template, i put it myself from jquery ui) so you can see the date you choosed, but in the backend i will send the date in 3 variables, day, month and yeat to the server and i will get an xml response and then with html and css i have to show the results , hotels, flight etc. so if you have a better idea (exept to change the form to have 3 fields for date) i?m all ears

Thkans again pals christianf and sycho

Link to comment
Share on other sites

ChristianF's solution is the one I would go with if you are taking the submitted value directly and parsing it out.

list($month, $day, $year) = explode('\', $_POST['CheckIn']);

 

It looks to me like the OP's datetime picker is sending DD/MM/YYYY ...

... from a datepicker in my web page  like this: 15/05/2012, ...

 

in which case, the PHP would be:

list($day, $month, $year) = explode('/', $_POST['CheckIn']);

(Why was that a backslash?)

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.