Passing birthdates in PHP

Homework giving you a headache? Math gives you a migraine? Can't quite figure out how to do something in photoshop? Never fear, the other members of CAA share their expertise in this forum.

Passing birthdates in PHP

Postby seaglass27 » Fri Dec 02, 2011 5:55 pm

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?
seaglass27
 
Posts: 494
Joined: Wed Aug 17, 2011 1:04 pm

Postby Mithrandir » Fri Dec 02, 2011 7:50 pm

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.
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.

Postby seaglass27 » Mon Dec 05, 2011 9:48 am

But how do I combine the three values into a single date?
seaglass27
 
Posts: 494
Joined: Wed Aug 17, 2011 1:04 pm

Postby Mithrandir » Mon Dec 05, 2011 8:26 pm

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);
User avatar
Mithrandir
 
Posts: 11071
Joined: Fri Jun 27, 2003 12:00 pm
Location: You will be baked. And then there will be cake.

Postby seaglass27 » Tue Dec 06, 2011 9:36 am

Oh, okay. Thanks a lot, Mith!
seaglass27
 
Posts: 494
Joined: Wed Aug 17, 2011 1:04 pm

Postby Slater » Tue Feb 07, 2012 4:18 am

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
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 28 guests