[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Calendar with select box

I have an events calendar, which stores events in a mysql database by timestamps. (1226469600 = 12 nov 2008)

Now I need a second calendar which checks the database for events when a user picks a date through 3 select boxes. 3 select boxes with day, month and year. When the select boxes are set, a result will show at the bottom. (no datepickers, I need 3 select boxes!)
Is there a possibility to return an error when the user picks a date that doesn't exist?
How do I check the timestamp from these 3 boxes and return the result at the bottom?

Any ideas?
1 Solution
Use the PHP checkdate() function [1] to validate a date.

[1] http://www.php.net/manual/en/function.checkdate.php
Roger BaklundCommented:
Use mktime() to calculate the timestamp, and then use date('Y-m-d') to check that the date was valid.

mktime() will accept invalid dates and transform it to a valid date. That's why you must use date() to check if the calculated date is the same as provided by the user. Remember to use two digits in the select dropdowns, i.e. 01, 02, 03 and so on.
$d = $_POST['day'];
$m = $_POST['month'];
$Y = $_POST['year'];
$date = mktime(0,0,0,$m,$d,$Y);
  echo 'Invalid date!';

Open in new window

i supose you want a javascript solution..
you can put a javascript on the submit button or on the select boxes

something like

var date1 = document.form.select1.value + "/" + ocument.form.select2.value + "/" +ocument.form.select3.value ;
var ret = isDate(date1, "d/m/Y"); //for 20/02/2008

rename date.js.txt in date.js
Hori76Author Commented:
Thanks! This is exactly what i needed!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now