473,326 Members | 2,090 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,326 software developers and data experts.

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 1935
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
5
by: Astra | last post by:
Hi All I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004 .... 2020), do you know of any js validation check I can use to check whether these values are older than...
1
by: panche | last post by:
I'm developing a fairly simple user control that has two textboxes for date/time entry (a from date/time and a to date/time). One of my requirements is that there should be no button that sets...
2
by: kiranmn75 | last post by:
I want to dynamically populate a combo box through javascript. Data is coming from a array. Sometimes data list may contain items in excess of 2000. Explorer takes more than 5 seconds to...
4
by: teknoshock | last post by:
I have created a page with multiple drop down boxes, all populated with the same options. My problem is, for 12 dropdown boxes and 40 choices per box, I end up with a massive file. Also, if I...
2
by: SONIQ | last post by:
Using javascripts to validate this form. Basic operation, when a user clicks the submit order button, the javascript code must validate everything entered by the user. Please help finnish this...
1
by: =?Utf-8?B?Ym9iYnk=?= | last post by:
I have a textBox Where I type date. I want to validate it. Im using customValidate controls. I have this function in my code behind page void DatesValidate(object source,...
7
by: mc | last post by:
I've not been able to get a Check box to client side validated on postback. with my changes I can get it to work on the serverside ok but not on the client. I have created a new control as...
8
vs2k8
by: vs2k8 | last post by:
Hello guys, New to this forum and new to access programing, my issue is I am comparing 2 date fields, I have to validate that Order Rcvd Dt should be less then Ord Comp date and Order Comp date...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.