Current location: Hot Scripts Forums » Programming Languages » PHP » date / time validation


date / time validation

Reply
  #1 (permalink)  
Old 04-20-08, 12:54 PM
mcrob mcrob is offline
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...
Reply With Quote
  #2 (permalink)  
Old 04-20-08, 01:08 PM
mab's Avatar
mab mab is offline
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???
Reply With Quote
  #3 (permalink)  
Old 04-20-08, 01:31 PM
mcrob mcrob is offline
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..

PHP Code:



// current date: april 20, 2008
$date mktime(0,0,0date("m"), date("d"), date("Y"));
//outputs this: 1208664000 
The current date gets entered into the database. Four days later the
product is now expired for example

PHP Code:



// four days later the $date will output this: 1209009600 
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)
Reply With Quote
  #4 (permalink)  
Old 04-20-08, 02:11 PM
mab's Avatar
mab mab is offline
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 -

Code:
SELECT * from date WHERE entered_date < DATE_SUB(CURDATE(),INTERVAL 4 DAY)
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???
Reply With Quote
  #5 (permalink)  
Old 04-20-08, 03:44 PM
mcrob mcrob is offline
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.


PHP Code:

SELECT from date WHERE entered_date DATE_SUB(CURDATE(),INTERVAL 4 DAY
On there it shows "interval 4 day", what kind of query could I use in that area?

could I do this?

PHP Code:

$currentdate date("m d Y");

SELECT from date WHERE entered_date DATE_SUB(CURDATE(),"$currentdate"
Reply With Quote
  #6 (permalink)  
Old 04-20-08, 03:53 PM
mab's Avatar
mab mab is offline
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???
Reply With Quote
  #7 (permalink)  
Old 04-20-08, 08:37 PM
mcrob mcrob is offline
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

PHP Code:

SELECT from test WHERE dateadded DATE_SUB(CURDATE(),INTERVAL 4 DAY
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.
Reply With Quote
  #8 (permalink)  
Old 04-20-08, 08:58 PM
mab's Avatar
mab mab is offline
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???
Reply With Quote
  #9 (permalink)  
Old 04-20-08, 09:19 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
perfect!!!! Learned something new now.. thank you!!
Reply With Quote
  #10 (permalink)  
Old 04-20-08, 09:37 PM
mcrob mcrob is offline
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.

PHP Code:

SELECT from test WHERE dateadded DATE_ADD(CURDATE(),INTERVAL 1 DAY
but it still shows....

April 29, 2008 and not April 21, 2008
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting date-string to Date object UnrealEd Everything Java 4 05-15-07 06:20 PM
having Loop issues, help... Advanced todayscoffee PHP 2 02-27-06 12:36 AM
date validation sharad JavaScript 0 09-21-04 09:39 AM
inserting and retrieving date and time from mysql database stealth04 PHP 5 07-29-04 11:19 AM
help regarding insertion of date time wajeeh_r ASP 1 03-04-04 04:05 PM


All times are GMT -5. The time now is 07:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.