469,638 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,638 developers. It's quick & easy.

Options for Validating a Date Value?

Surprisingly (at least to me), there doesn't seem to be a built-in
function to validate a date value (like, say, is_date()). Given that,
is there a best practice for determining whether a value is a valid
date/time? The values I need to test will likely be unix timestamp
values and I need to be able to distinguish them as date/time values
from other integer/numeric values.

What I'm trying to do is use reflection to iterate over the properties
of an object and insert them into database fields based on their type
(integers in an INT field, strings in a VARCHAR field, date/time
values in a DATETIME field). Most are fairly straightforward, but I'm
not sure how to consistently and accurately identify a datetime value.

Any thoughts would be appreciated.

Rob
Nov 24 '07 #1
8 1780
Rob Wilkerson wrote:
Surprisingly (at least to me), there doesn't seem to be a built-in
function to validate a date value (like, say, is_date()). Given that,
is there a best practice for determining whether a value is a valid
date/time? The values I need to test will likely be unix timestamp
values and I need to be able to distinguish them as date/time values
from other integer/numeric values.

What I'm trying to do is use reflection to iterate over the properties
of an object and insert them into database fields based on their type
(integers in an INT field, strings in a VARCHAR field, date/time
values in a DATETIME field). Most are fairly straightforward, but I'm
not sure how to consistently and accurately identify a datetime value.

Any thoughts would be appreciated.

Rob
Unix timestamps are just integers. You can't differentiate them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 24 '07 #2
On Nov 24, 3:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>
Unix timestamps are just integers. You can't differentiate them.
Yeah, I was hoping that you all (as seasoned php developers) had
figured out some fancy technique for identifying one as a date value
rather than as an integer. :-) Guess I'll have to come up with a new
plan...

Thanks.
Nov 24 '07 #3
What about first converting the integer to a date using the "date"
function. From here you can see if it validates to a real date by
using PHP's built-in "checkdate" function, and then go from there. You
will have to do additional testing if the converted integer from the
first step turns out to be "12-31-1969," as most numbers 5-digits or
less will.

ie

date('m-d-Y',12345) returns "12-31-1969"


On Nov 24, 2:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Rob Wilkerson wrote:
Surprisingly (at least to me), there doesn't seem to be a built-in
function to validate a date value (like, say, is_date()). Given that,
is there a best practice for determining whether a value is a valid
date/time? The values I need to test will likely be unix timestamp
values and I need to be able to distinguish them as date/time values
from other integer/numeric values.
What I'm trying to do is use reflection to iterate over the properties
of an object and insert them into database fields based on their type
(integers in an INT field, strings in a VARCHAR field, date/time
values in a DATETIME field). Most are fairly straightforward, but I'm
not sure how to consistently and accurately identify a datetime value.
Any thoughts would be appreciated.
Rob

Unix timestamps are just integers. You can't differentiate them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Nov 24 '07 #4
>What about first converting the integer to a date using the "date"
>function. From here you can see if it validates to a real date by
using PHP's built-in "checkdate" function, and then go from there. You
I believe this check will *always* pass if the number fits in a 32-bit
integer, rendering the check somewhat pointless.

Do you have any additional constraints on this date other than that
it's a valid date? Like it's supposed to be in the future, in the
past, or reasonably close (e.g. within a year) of the current time?
Nov 24 '07 #5
Rob Wilkerson wrote:
On Nov 24, 3:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Unix timestamps are just integers. You can't differentiate them.

Yeah, I was hoping that you all (as seasoned php developers) had
figured out some fancy technique for identifying one as a date value
rather than as an integer. :-) Guess I'll have to come up with a new
plan...

Thanks.
Nope, there is no difference.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 24 '07 #6
Rob Wilkerson wrote:
On Nov 24, 3:56 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Unix timestamps are just integers. You can't differentiate them.

Yeah, I was hoping that you all (as seasoned php developers) had
figured out some fancy technique for identifying one as a date value
rather than as an integer. :-) Guess I'll have to come up with a new
plan...

Thanks.
Can you tell what your INT data is going to be? Can you tell what is
the oldest date is going to be? You could possibly hack it if your
integer data falls within a certain range you could make some assumptions:

If each of your INTeger data is less than 1000000000 (ten digits, 1
billion) which is a unix timestamp equal to Sept. 08, 2001 @ approx
9:46pm, then you could possibly assume that anything above this would be
a timestamp.

Ugly I know...

Norm
Nov 25 '07 #7
NC
On Nov 24, 12:44 pm, Rob Wilkerson <r.d.wilker...@gmail.comwrote:
>
Surprisingly (at least to me), there doesn't seem to be a built-in
function to validate a date value (like, say, is_date()). Given that,
is there a best practice for determining whether a value is a valid
date/time? The values I need to test will likely be unix timestamp
values and I need to be able to distinguish them as date/time values
from other integer/numeric values.
A Unix timestamp is an integer, so any integer is by definition a
valid Unix timestamp and thus requires no validation.

Cheers,
NC

Nov 25 '07 #8
On Nov 25, 12:25 am, NC <n...@iname.comwrote:
A Unix timestamp is an integer, so any integer is by definition a
valid Unix timestamp and thus requires no validation.
In my case, I wasn't really looking to validate the value as much as
distinguish it as a date/time value. After reading the feedback
everyone provided, I came to realize that it doesn't matter. It
doesn't really matter how I persist the value as long as the class
instance itself knows what to do with the value when it's retrieved.
I was over-thinking things a bit. :-)
Nov 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Astra | last post: by
2 posts views Thread by kiranmn75 | last post: by
4 posts views Thread by teknoshock | last post: by
2 posts views Thread by SONIQ | last post: by
1 post views Thread by =?Utf-8?B?Ym9iYnk=?= | last post: by
7 posts views Thread by mc | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.