Page 1 of 1

Passing birthdates in PHP

PostPosted: Fri Dec 02, 2011 5:55 pm
by seaglass27
I want to allow users to give their birthdate on my site with dropdown boxes (one each for day, month, and year). How should I code this in PHP?

PostPosted: Fri Dec 02, 2011 7:50 pm
by Mithrandir
I've always used a select box with one item to do drop-down menus. Just do three of those and read it as a standard HTTP form post.

PostPosted: Mon Dec 05, 2011 9:48 am
by seaglass27
But how do I combine the three values into a single date?

PostPosted: Mon Dec 05, 2011 8:26 pm
by Mithrandir
I'd use something like this, probably...

$year = $_REQUEST{'year'};
$month = $_REQUEST{'month'};
$day = $_REQUEST{'day'};
$birthday = $day . "/" . $month . "/" . $year;

If you wanted to be fancy you could use something like the implode() function:

$year = $_REQUEST{'year'};
$month = $_REQUEST{'month'};
$day = $_REQUEST{'day'};
$all_bday = array($day, $month, $year);
$birthday = implode("/", $all_bday);

PostPosted: Tue Dec 06, 2011 9:36 am
by seaglass27
Oh, okay. Thanks a lot, Mith!

PostPosted: Tue Feb 07, 2012 4:18 am
by Slater
And if you want to go the extra mile, try dabbling around with Javascript's Date class. It's pretty flexible, for example...

Code: Select all
var d = new Date("6/13/1986");


You can create a new Date instance as simple as that, with just a string. In this case, I'm creating a Date object for June 13, 1986. From here, you can do things like...

Code: Select all
var dateString = d.toDateString();
alert(dateString);


This will print a pretty nice string to the alert box for your date: "Fri Jun 13 1986".

You can also do data inspection on the date. Maybe you want to format the string in your own way?

Code: Select all
var dayOfWeekStr;
var monthStr;
var twoDigitYear;

switch(d.getDay()) {
    case 0:
        dayOfWeekStr = "Sunday";
        break;
    case 1:
        dayOfWeekStr = "Monday";
        break;
    case 2:
        dayOfWeekStr = "Tuesday";
        break;   
    case 3:
        dayOfWeekStr = "Wednesday";
        break;
    case 4:
        dayOfWeekStr = "Thursday";
        break;
    case 5:
        dayOfWeekStr = "Friday";
        break;
    case 6:
        dayOfWeekStr = "Saturday";
        break;
    default:
        dayOfWeekStr = null;
        break;
}

switch (d.getMonth()) {
    case 0:
        monthStr= "January";
        break;
    case 1:
        monthStr= "February";
        break;
    case 2:
        monthStr= "March";
        break;
    case 3:
        monthStr= "April";
        break;
    case 4:
        monthStr= "May";
        break;
    case 5:
        monthStr= "June";
        break;
    case 6:
        monthStr= "July";
        break;
    case 7:
        monthStr = "August";
        break;
    case 8:
        monthStr= "September";
        break;
    case 9:
        monthStr= "October";
        break;
    case 10:
        monthStr = "November";
        break;
    case 11:
        monthStr= "December";
        break;
    default:
        monthStr = null;
        break;
}

twoDigitYear = (twoDigitYear = d.getYear()%100) > 10 ?
                     "" + twoDigitYear : "0" + twoDigitYear;

alert("The date was " + monthStr + " " + d.getDate() + ", '" + twoDigitYear + ". That day was a " + dayStr);

An example of that (with the present date) here: http://jsfiddle.net/nbyrd/Y2F8J/1/

And of course the sky's the limit with what you do with those values?

Working with JS Dates is a really good idea; most (if not all) major deserializers out there are equipped to deal with them seamlessly, and you can pass them as form data to just about any page for post-processing.

EDIT:
It's also pretty flexible and will simplify your life by taking care of those "weird" cases... :)

Code: Select all
d = new Date("2/31/2012"); // yields: 2 Mar 2012