
04-20-08, 12:54 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
date / time validation
Hello!
I would like to do a php date / time validation. Basically I want to enter the
day, month, year in the database, then have a code that does the calculation of how many days it has been since that stored date. So for example, A new user registered on april 10, 2008 on a website, and now todays date is april 20, 2008 for example, meaning this user has been registered for 10 days. I want to be able to find out how many days it has been since that user has been registered.
A user added a product to there shopping cart on April 28, 2008 and today's date is May 2, 2008, it has been 4 days and the entered product in the shopping cart has expired, I want to make a code where it calculated the amount of days it has been before it gets deleted.
I hope this makes sense...
|

04-20-08, 01:08 PM
|
 |
Community VIP
|
|
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You can use the mysql datediff() function to find the number of days between a DATE or DATETIME data type in your database and any other date or datetime value.
Ref: http://dev.mysql.com/doc/refman/5.0/...ction_datediff
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
|

04-20-08, 01:31 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is what I was thinking, I was wondering if I could do this..
The current date gets entered into the database. Four days later the
product is now expired for example
Could I make it in the database so it does
SELECT * from date WHERE entered_date < '$date' (april 24, 2008
date, outputs 1209009600) where the entered date was april 20, 2008
1209009600)
|

04-20-08, 02:11 PM
|
 |
Community VIP
|
|
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ummm. Your statement of what you want changed from "find the number of days between dates" to "select the rows more than 4 days ago."
Note: The following assumes that you are using a mysql DATE data type.
To select rows more than 4 days ago, just form the date and compare it in your query -
The reason I mentioned using a DATE data type is because a Unix timestamp requires a conversion or several slow php statements at some point to use it for any purpose other than to sort in ascending or descending order. A DATE data type allows you to use about 20-30 mysql functions directly in a query and you can output it in any format you want using the mysql DATE_FORMAT() function.
You can use a mysql FROM_UNIXTIME() function in your query to produce a standard DATE value from a Unix timestamp, but this also slows everything down over just using a DATE column directly.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
|

04-20-08, 03:44 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
wow Im going to try this! This seems like an interesting function.
On there it shows "interval 4 day", what kind of query could I use in that area?
could I do this?
|

04-20-08, 03:53 PM
|
 |
Community VIP
|
|
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The link in post #2 in this thread takes you to the mysql manual section where all the date and time functions are documented.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
|

04-20-08, 08:37 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
this works great!!! I have some questions tho.
Yes I learned in order for this function to work, the data type in the table has to be DATE and it is formatted
year,month,day.
I made it so the date is 2008-04-16 as a test and I did this code
I did the practice test in phpmyadmin. from april 16 2008 to april 20, 2008 is 4 days. why is it that it wouldnt show the results if it was the past 4 days but if I changed it to interval 3 day it would show? It doesnt make much sense to me
Last edited by mcrob; 04-20-08 at 08:39 PM.
|

04-20-08, 08:58 PM
|
 |
Community VIP
|
|
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Because the test is less than <, which translates to more than 4 days ago. If you want 4 or more days ago, use less than or equal to <=
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
|

04-20-08, 09:19 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
perfect!!!! Learned something new now.. thank you!!
|

04-20-08, 09:37 PM
|
|
Coding Addict
|
|
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sorry another question since Im doing some studying on these functions.
Instead Im trying out the date_add function. I only wanted to pull up the records for 1 day later this is what I tried.
but it still shows....
April 29, 2008 and not April 21, 2008
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|