473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Date validation

250 100+
I want to validate Date in my php application. User should select a date by clicking on calander icon or typinig. I want to check whether the date is in "2008-07-01" format. How can i do that? Could some one help me?
Jul 23 '08 #1
3 1360
r035198x
13,262 8TB
You could use a simple regular expression for that validation.
Jul 23 '08 #2
try this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $date_in = '2008-07-01';
  3. list($year, $month, $day) = explode('-', $date_in);
  4.  
  5. if (checkdate($month, $day, $year)) {
  6.   echo 'date valid.';
  7. } else {
  8.   echo 'date invalid';
  9. }
  10.  
Jul 23 '08 #3
dlite922
1,584 Expert 1GB
try this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $date_in = '2008-07-01';
  3. list($year, $month, $day) = explode('-', $date_in);
  4.  
  5. if (checkdate($month, $day, $year)) {
  6.   echo 'date valid.';
  7. } else {
  8.   echo 'date invalid';
  9. }
  10.  
nice, but regex would be faster.

Here's one that checks for February dates range 0-29:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. preg_match("/^(19|20)?[0-9]{2}[-/ \\.]((0?[13-9]|1[012])[-/ \\.](0?[1-9]|[12][0-9]|3[01])|(0?2[-/ \\.][012]?[0-9]))$/",$variable); 
  5.  
  6.  
if you don't care about February 30 / 31 date checking try this:

Expand|Select|Wrap|Line Numbers
  1. preg_match("/^(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])$/",$variable);
  2.  
  3.  
please let me know if there's any problems in the first one, I hand coded it myself.


-Dan
Jul 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
0
by: Brian Conway | last post by:
I am having some validation and insertion problems. I am using a date picker that takes the selected date and puts it to ("dd-MMM-yyyy") format, as this was the only format that Oracle would...
7
by: Paul | last post by:
Hi, I have a form where a user is required to enter a start date and an end date. Both are required and must between a specific date range (e.g. 01/01/1900 and 01/01/2099) and the end date...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
by: Diego | last post by:
Can I validate (possibly with a compare validator) a Date entered by the user based upon his regional settings? I.e. if a user is american the format would be mm/dd/yyyy, if brittish dd/mm/yyyy...
1
by: Brendan Reynolds | last post by:
In an ASP.NET 1.1 app I have the following range validation control. This is an intranet app that will be used only within Ireland, so all date input is expected to be in dd/mm/yyyy format. ...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
3
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I have VS 2005 (C#) There is a control numericUpDown so you can spin numeric values. What I need to do is to spin date (+- one day). How to do that? Moreover, I want a user to type the date as...
2
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.