cpcdev
cpcdev

Reputation: 1214

Split Birth Date

I have some birth dates stored in mm/dd/yyyy format. Ex: 05/20/1987

I have a form to edit the birth date and I would like to split up the month, day, and year so I can echo them separately in some select drop downs.

<?php $birthdate = "05/20/1987"; ?>

<select name="dob_month">
<option value="<?php echo $month; ?>"></option>
</select>

<select name="dob_day">
<option value="<?php echo $day; ?>"></option>
</select>

<select name="dob_year">
<option value="<?php echo $year; ?>"></option>
</select>

So with the birthdate given above, it would look like this:

<select name="dob_month">
<option value="05"></option>
</select>

<select name="dob_day">
<option value="20"></option>
</select>

<select name="dob_year">
<option value="1987"></option>
</select>

Is regex the correct solution?

Upvotes: 0

Views: 293

Answers (3)

zx81
zx81

Reputation: 41838

If your format is fixed and you don't need to validate the date, you can directly use substr:

echo substr($birthdate,0,2); // month
echo substr($birthdate,3,2); // day
echo substr($birthdate,6,4); // year

Upvotes: 0

codephobia
codephobia

Reputation: 1590

list($month, $day, $year) = explode('/', $birthdate);

Upvotes: 4

hsz
hsz

Reputation: 152216

Just try with:

list($month, $day, $year) = explode('/', $birthdate);

Upvotes: 4

Related Questions