Forum Moderators: coopster

Message Too Old, No Replies

From select to date("Y-m-d",?)

date form select drop down

         

Gruessle

11:27 am on Feb 21, 2005 (gmt 0)

10+ Year Member




I am going nuts here.

I have 3 select forms for a birtday

<select name="bmonth">
<select name="bday">
<select name="byear">

$birthday = date("Y-m-d",(int)$_POST['byear'].'-'.(int)$_POST['bmonth'].(int)$_POST['bday']);

Date registerd is allways "1969-12-31" no matter what I select. Why?

jatar_k

6:04 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because the date function expects a timestamp

the conversion should be uneccessary altogether if you pass the proper values from the dropdowns. You could then just implode them with a -

Gruessle

10:49 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



I think that implode thing should work. I read this [us4.php.net...] but need more help.

There are more details about my select boxes below.

$birthday = date("Y-m-d",(int)$_POST['byear'].'-'.(int)$_POST['bmonth'].'-'.(int)$_POST['bday']);

<select name="bmonth">
<option value="" selected>Month</option>
<option value=01>January</option>
<option value=02>February</option>

<select name="bday">
<option value="" selected>Day</option>
<option value=01>01</option>
<option value=02>02</option>

<select name="byear">
<option value=1890>1890</option>
<option value=1891>1891</option>
<option value=1892>1892</option>

jatar_k

11:52 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually, you could just do this

$birthday = $_POST['byear'].'-'.$_POST['bmonth'].'-'.$_POST['bday'];

Gruessle

1:07 am on Feb 22, 2005 (gmt 0)

10+ Year Member



I tried that and it doesn't work. So I made a litle test script and all dates before 1970 should have a minus sign but they don't so the dates come out wrong.

What can I do?

$convert = strtotime("2004-01-04");
$settime = $convert;
echo "settime: " . $settime . "<br />";
echo "formatted: " . date("F j, Y, g:i a",$settime) . "<p />";

Gruessle

1:13 am on Feb 22, 2005 (gmt 0)

10+ Year Member



No it does work, I mean what you gave me but why does my script return wrong dates before 1969

About your fix?
Why does it work without (int) and not with?

jatar_k

2:13 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no need for (int) I also removed the date() which was the real reason it didn't work before.

if your function returns a date in 1969 then it almost always means bad timestamp

the real way to do what you are trying to do would be

$birthday = date("Y-m-d",mktime(0,0,0,$_POST['bmonth'],$_POST['bday'],$_POST['byear']));

using mktime to convert the data you get from the form/user to create a timestamp and then pass that into date to get the appropriate format.