-1

The datepicker I'm using passes the date to a php script on form submit.

The URL parameter is as follows - datepicker1=12%2F09%2F2014

I can store 12%2F09%2F2014 in a variable.

$datepicker1 = $_GET['datepicker1'];

I also have 3 other variables namely: $day $month $year

How do add individual day, month, and year to their respective variables by extracting them from variable $datepicker1?

Cody Raspien
  • 1,753
  • 5
  • 26
  • 51
  • Are you sure `$datepicker1` contains `%2F` sequences? They appear in the URL because `/` is a special character in URLs but they should be decoded when `$_GET[]` is populated. Use `DateTime::createFromFormat('d/m/Y', $datepicker1);` to work with the value. – axiac Dec 04 '14 at 12:11
  • Yes, I'm using the JQuery datepicker. I can urldecode the variable to get 12/09/2014. But then, how do I get 12 to $day, 09 to $month, and 2014 to $year? – Cody Raspien Dec 04 '14 at 12:12

4 Answers4

1

Try this:

$datepicker1 = "12%2F09%2F2014";
$val =  urldecode($datepicker1);
$array = explode('/', $val);
print_r($array);

The $array variable is your result. Now you can assign array value into your $day, $month, $year variable like this:

$day = $array[0];
$month = $array[1];
$year = $array[2];

Now you can store it into database. I hope this will helpful for you. Thanks

Yash
  • 1,446
  • 1
  • 13
  • 26
0

use this urldecode() function

$date =  urldecode("12%2F09%2F2014");

$year = date('Y', strtotime($date));
$mon = date('m', strtotime($date));
$day = date('d', strtotime($date));
navdbaloch
  • 191
  • 12
  • Once I have 12/09/2014 from decode, how do I add 12 to $day, 09 to $month, and 2014 to $year? Is it possible to use trim? I'm looking for the best solution to do this. – Cody Raspien Dec 04 '14 at 12:11
0

You Can use sbstr() to get the value.

Suppose for date $date=substr($datepicker1,0,2);

Similarly others.. :)

-1
$date= urldecode("12%2F09%2F2014");
echo substr($date, 0, 2);
echo "<br>";

echo substr($date, 3, 2);
echo "<br>";

 echo substr($date, 6, 4);
 echo "<br>";

I tried the above and it seems to work fine for me.

Instead of echo-ing - will add these to variables.

The array solution provided by Yash also works.

Cody Raspien
  • 1,753
  • 5
  • 26
  • 51