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

Subtract/Add to date to get a usable variable

4
I know the title is misleading. I am working on a class project where we are trying to setup a notification system written in PHP and using an MySQL database. We are trying to set-up a notification page that will show what users are nearing expiration. The expiration dates are entered in manually and are not a timestamp. The dates are entered in on another page into the SQL database. Then what we need is to be able to take those dates and find that if they are near (lets say) 7 months to the date entered then it displays the users name that is about to expire.

I have tried different combinations of date(), strtotime(), date(mktime()), dateadd(), strftime(), etc.... and have been unsuccessful. All of those functions work if the date is physically a part of the code or off of the current time. But for some reason I cannot get it to subtract from a date in the database without it returning 12-31-1970.

Any and all help is greatly appreciated and thanks in advance.

(also sorry for the long post, just wanted to be thorough)
Feb 25 '08 #1
6 4833
ronverdonk
4,258 Expert 4TB
Have you tried to do it from the SQL (this is using MySQL) side? I don't know what db you are using but you can select data from the db calculating the difference between the user-entered-date and the sql-table-date. Something like
Expand|Select|Wrap|Line Numbers
  1. $user_date=...whatever the date entered
  2. SELECT * FROM table WHERE DIFFDATE($user_date, sql_date) < 100
Ronald
Feb 25 '08 #2
lptl
4
Thank you for the quick response!
I'm trying to run the query inside MySQL but I'm having trouble. I tried using other numbers (assuming that 100 = 1 month if I read correctly) and the opposite sign > and it keeps returning:

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010 sec)

when I try to run it using phpMyadmin. There is 5 rows of user data with dates in the column i'm trying to use. Any ideas?

Kyle

*Update
also here is my server stats:

Apache version 2.0.61
PHP version 5.2.5
MySQL version 4.1.22-standard
Feb 26 '08 #3
ronverdonk
4,258 Expert 4TB
Then show your code (relevant PHP and MySQL) (within code tags) and we know what we are talking about.

Ronald
Feb 26 '08 #4
lptl
4
I think this is what you asked for. Within PHP this is how I call to my db and pull data to display on the web:
Expand|Select|Wrap|Line Numbers
  1. <?php include '../config.php'; 
  2. $query = "SELECT user_id, userFirstName, userMiddleInitial, userLastName FROM users ORDER BY userLastName"; 
  3. $result = mysql_query($query) or die('Error, select query failed');                
  4. $row = mysql_fetch_array($result, MYSQL_ASSOC)
  5. ?>
Then when displaying I have it running through a loop to display all users in the table formatted like:
Expand|Select|Wrap|Line Numbers
  1. print "{$row["userLastName"]}, {$row["userFirstName"]} {$row["userMiddleInitial"]}";
For the date that needs to be subtracted from, I tried using your suggestion: (but returns 0 results)
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT *, DATE_FORMAT(userDateExpire, '%m-%d-%Y') AS userDateExpire FROM users WHERE DATEDIFF('userDateExpire','sql_date') < 700 ORDER BY userLastName";
I have used the following example but had no success either: (also tried various forms of the styles noted in my first post)
Expand|Select|Wrap|Line Numbers
  1. $today = date('m-d-Y', mktime(0, 0, 0, date("m")-7, date("d"), date("Y")));
I know this takes today's date and will subtract 7 months but when I have tried to use my date, 'userDateExpire' in place of 'm', 'd', 'Y' in mktime(), it always returns 12-31-1970 or 01-31-1969. I know that this date is the UTS but unsure of how it calls that instead of the values within my table?

I think this is everything that you had asked to see. Thank you again for your continuing help.

Kyle
Feb 26 '08 #5
ronverdonk
4,258 Expert 4TB
Let's hold it a sec! The way MySQL does date calculations is ALWAYS on the YYYY-MM-DD format. So, in whatever format your date is, you must have it (or convert it) to the YYYY-MM-DD format before you do the calculations!

So userDateExpire (in your table having data type DATE or DATETIME) is already in that format.

The date that the user passes must be converted to the YYYY-MM-DD format (using DATE_FORMAT) before doing the DATE_DIFF calculation.

Recap: when the 'userDateExpire' field in your db has format YYYY-MM-DD.
When the user-entered date (assume in php variable $userdate) has format DD-MM-YYYY, you must reformat it to YYYY-MM-DD.
Then you SQL statement would be .e.g
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM users 
  2.           WHERE DATEDIFF(userDateExpire,DATE_FORMAT('$userdate', '%d-%m-%Y')) < 700 
  3.           ORDER BY userLastName";
Ronald
Feb 26 '08 #6
lptl
4
Yep, I understand the SQL stores the date in YYYY-MM-DD format. The userDateExpire is the data type DATE with default value of 0000-00-00. Dates manually entered into the database in the userDateExpire column are entered in the format YYYY-MM-DD.
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT *, DATE_FORMAT(userDateExpire, '%m-%d-%Y') AS userDateExpire FROM users WHERE DATEDIFF('userDateExpire','sql_date') < 700 ORDER BY userLastName";
In the above code, the DATE_FORMAT is being used to convert the data for display only. I researched into DATEDIFF() a little bit more and came up with the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, DATE_FORMAT(userDateExpire, '%m-%d-%Y') AS userDateExpire FROM users WHERE DATEDIFF(userDateExpire, CURDATE()) <= 213 ORDER BY userLastName
I also found that the number in the WHERE statement, ex. 700, is the number of days not 7 months. 7 months is equal to 213 days (give or take a few decimal points).

This works and displays my data correctly. I also found that when using column names in the DATEDIFF() function there should not be quotes around the column name(s). I think that was my biggest problem when I was running the query.

Thank you again for all of your help!

Kyle
Feb 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
3
by: S. van Beek | last post by:
Dear reader, With DatePart() you can subtract the year or the week from a date field with: DatePart("yyyy";) for the year
5
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out...
7
by: Jerome | last post by:
Hallo, I know a lot has already been told about date/time fields in a database but still confuses me, specif when dealing with SQLserver(Express). It seems that sqlserver only accepts the date in...
8
by: Remington | last post by:
I am using windows 2000pro with access 2000. I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select...
3
by: Arne Gemsa | last post by:
Hi, I want to get a starttime. To get this I have to subtract the runtime from a unit e.g. 08:40:15 from a date e.g. 2007-03-29 16:48:30. So the starttime is 2007-03-29 08:08:15. Is there any...
2
by: jld730 | last post by:
Hello All, I am very new to Python and Oracle, and I have a question for you all. How do you add/subtract minutes to a date? Is this possible? I need to add/subtract 30 minutes (or 60, or 90,...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.