0

I want to do my registration like in Facebook. There is 3 dropdownlist where I choose year, day and month. I finished 3 function which return me (31 days, 12 months and years from 1915-today). Now I have one problem when I parse string to date. This is my code:

public function getDate() {
    $month = $this->Month;
    return $this->Year . '-' . $month . '-' . $this->Day;
}

public function getDbFormatedDate() {
    $dateDeadline = date_create($this->getDate());
    return date_format($dateDeadline, 'Y-m-d');
}

for example when I take

31 February 2016

. In my database this code save date

2 March 2016

. I think maybe I should compare my dates and when user click 31 February 2016 some function should compare this date with final date and return message that this date does not exist. Look at calendar for next example when I click

31.04.2016

this date doesn't exist and my code save in base

1.05.2016.

Anyone have idea how to resolve this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Informer
  • 195
  • 1
  • 13

1 Answers1

1

First off, it doesn't have anything to do with the leap year - other than that February could have 28 or 29 days depending on which year is chosen. But other months have different number of days as well, so you'll need to always validate - not just during leap year.

To validate the date is valid on the PHP side, use the technique demonstrated in this answer.

Additionally, you may want to consider creating a more friendly user experience, by automatically adjusting the number of days available based on the selected year and month. When you do, place the controls in year-month-day order for optimal user experience.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575