Options for Validating a Date Value?

Options for Validating a Date Value?

am 24.11.2007 21:44:06 von Rob Wilkerson

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

Re: Options for Validating a Date Value?

am 24.11.2007 21:56:19 von Jerry Stuckle

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.
jstucklex@attglobal.net
==================

Re: Options for Validating a Date Value?

am 24.11.2007 22:06:20 von Rob Wilkerson

On Nov 24, 3:56 pm, Jerry Stuckle wrote:
>
> 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.

Re: Options for Validating a Date Value?

am 24.11.2007 22:16:45 von Acrobatic

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 wrote:
> 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
> ==================

Re: Options for Validating a Date Value?

am 24.11.2007 22:40:21 von gordonb.z700b

>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?

Re: Options for Validating a Date Value?

am 24.11.2007 22:49:12 von Jerry Stuckle

Rob Wilkerson wrote:
> On Nov 24, 3:56 pm, Jerry Stuckle wrote:
>> 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.
jstucklex@attglobal.net
==================

Re: Options for Validating a Date Value?

am 25.11.2007 02:08:53 von Norman Peelman

Rob Wilkerson wrote:
> On Nov 24, 3:56 pm, Jerry Stuckle wrote:
>> 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

Re: Options for Validating a Date Value?

am 25.11.2007 06:25:19 von nc

On Nov 24, 12:44 pm, 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.

A Unix timestamp is an integer, so any integer is by definition a
valid Unix timestamp and thus requires no validation.

Cheers,
NC

Re: Options for Validating a Date Value?

am 25.11.2007 19:44:37 von Rob Wilkerson

On Nov 25, 12:25 am, NC wrote:
> 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. :-)