473,386 Members | 1,758 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,386 software developers and data experts.

Date validation issue

Hi guys

I'm having a hard time trying to validate some dates.
I have a form that accept dates from users in this format (YYYY-mm-dd).
I've been looking at the available datetime functions and I'm still
clueless as to how they can help me achieve my goals.

what I need to do id check is:
1- convert the input string into date first. At this point I couldn't
find any srtTOdate type of functions. then I tried doing it the hard
way...by using "checkdate" function. I could split the string fine
(using "explode") but then I was stuck because I couldn't find any
function to convert a string into Integer!

2- I need to check if the date is valid
Could somebody please help me out?

Thanks

Jul 17 '05 #1
5 4997
Hello,

on 06/27/2005 01:04 AM nd******@gmail.com said the following:
I'm having a hard time trying to validate some dates.
I have a form that accept dates from users in this format (YYYY-mm-dd).
I've been looking at the available datetime functions and I'm still
clueless as to how they can help me achieve my goals.

what I need to do id check is:
1- convert the input string into date first. At this point I couldn't
find any srtTOdate type of functions. then I tried doing it the hard
way...by using "checkdate" function. I could split the string fine
(using "explode") but then I was stuck because I couldn't find any
function to convert a string into Integer!

2- I need to check if the date is valid


You may want to take a look at this popular forms generation and
validation class that comes with custom input plug-in that lets the user
enter a date that is validated either on the client side with Javascript
and the server side with the class PHP code.

Not only it can validate the date, but also impose optional requirements
to accept dates after a given start date or before a end date.

http://www.phpclasses.org/formsgeneration

Here is a screenshot of an example date form:

http://www.phpclasses.org/browse/file/8245.html
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #2


nd******@gmail.com wrote:
Hi guys

I'm having a hard time trying to validate some dates.
I have a form that accept dates from users in this format (YYYY-mm-dd).
I've been looking at the available datetime functions and I'm still
clueless as to how they can help me achieve my goals.

what I need to do id check is:
1- convert the input string into date first. At this point I couldn't
find any srtTOdate type of functions. then I tried doing it the hard
way...by using "checkdate" function. I could split the string fine
(using "explode") but then I was stuck because I couldn't find any
function to convert a string into Integer!

2- I need to check if the date is valid
Could somebody please help me out?

Thanks


using checkdate() sounds good to me. converting a string into an int
works like that:

$int = (int)$string;

micha

Jul 17 '05 #3
*** nd******@gmail.com wrote/escribió (26 Jun 2005 21:04:47 -0700):
1- convert the input string into date first. At this point I couldn't
find any srtTOdate type of functions.
In the "Date and Time Functions" chapter of the PHP manual you have all
date and time functions, including strtotime()
then I tried doing it the hard way...by using "checkdate" function. I could split the string fine
(using "explode") but then I was stuck because I couldn't find any
function to convert a string into Integer!

2- I need to check if the date is valid


As you already mentioned, checkdate sounds like a good option.
You may also find useful this code for a previous additional client-side
validation:

/*
* Returns true if valid date, false otherwise
* Month range is [1,12]
*/
function is_date(day, month, year // v2005-03-14
var TheDate=new Date();
TheDate.setFullYear(year, month-1, day);

return (TheDate.getDate()==day) && (TheDate.getMonth()==month-1) &&
(TheDate.getFullYear()==year);
}
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #4
Thanks for all the replys
It's a lot of help.

Jul 17 '05 #5
NC
nd******@gmail.com wrote:

I'm having a hard time trying to validate some dates.
I have a form that accept dates from users in this format
(YYYY-mm-dd). I've been looking at the available datetime
functions and I'm still clueless as to how they can help
me achieve my goals.


function is_valid_date($datestring) {
if (date('Y-m-d', strtotime($datestring)) == $datestring) {
return true;
} else {
return false;
}
}

Usage examples:

is_valid_date('2005-05-22'); // returns true
is_valid_date('2005-05-32'); // returns false

The function assumes leading zeros are mandatory. If they
are not, here's a revision that will fix it:

function is_valid_date($datestring) {
list($y, $m, $d) = explode('-', $datestring);
list($yy, $mm, $dd) = explode('-', date('Y-m-d',
strtotime($datestring)));
if (($y == $yy) and ($m == $mm) and ($d == $dd)) {
return true;
} else {
return false;
}
}
Cheers,
NC

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
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...
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...
0
by: pgharat | last post by:
Hello, I am working on Excel 2007 VBA. I have around 7-8 excel 2007 template in which using VBA added all functionality. In that some of the excel templates i m facing some issue of number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.