Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Get the difference between current date and date stored in database
Old 06-26-2008, 09:06 AM Get the difference between current date and date stored in database
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Hi,

I'm trying to get the difference between the current date and the date stored in the database. I have attempted to calculate the difference and made the php current date the same format as the date in the database and it doesn't work, can anyone see why?

PHP Code:
$date1 date('Y-m-j'); // yyyy-mm-dd
$date2 $actionId['dueDate'];
echo 
$date1;
echo 
$date2;
 
$calculateDiff $date1 $date2;
echo 
$calculateDiff;
$dateDiff floor($dateDiff/(60*60*24));   echo " Differernce is $dateDiff days"
Thanks,

Last edited by drew22299; 06-26-2008 at 09:08 AM..
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-26-2008, 09:47 AM Re: Get the difference between current date and date stored in database
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Well, the string representing the date is not a normal number, as there are 12 months in a year, and approximately 30 days in a year. Normal math does not apply. You will need to obtain a serial number (I believe a Unix timestamp will work), then convert the result of the difference to whatever date format you wish.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-26-2008, 09:51 AM Re: Get the difference between current date and date stored in database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
For the easy solution, on mysql, I would do it that way (!! works only if you want the years alone, months/days computing are messed due to leap years !!):
http://ma.tt/2003/12/calculate-age-in-mysql/
Code:
mysql> SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS('1985-02-08 21:28:45')), '%y')AS age;
+-------------------------------------+
| age                                 |
+-------------------------------------+
| 23 |
+-------------------------------------+
1 row in set (0.00 sec)
And if you can use postgresql, use it. It's better than mysql on so many points:
Code:
pgsql => select age(now(),'1985-02-08 21:28:45');
                   age
-----------------------------------------
 23 years 4 mons 17 days 18:17:54.229186
(1 row)
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-26-2008 at 09:55 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-29-2008, 12:25 PM Re: Get the difference between current date and date stored in database
Novice Talker

Posts: 14
Name: Trafalgar Law
Trades: 0
MySQL4 use
Quote:
SELECT * FROM tables WHERE (TO_DAYS( NOW( ) ) - TO_DAYS( Stored_Date )>1)
for mySQL4.1+ you can use DATEDIFF(NOW(),Stored_Date)
Trafalgar is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 06:22 AM Re: Get the difference between current date and date stored in database
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Quote:
Originally Posted by tripy View Post
For the easy solution, on mysql, I would do it that way (!! works only if you want the years alone, months/days computing are messed due to leap years !!):
http://ma.tt/2003/12/calculate-age-in-mysql/
[/code]
Thanks for your comments everyone tripy, is there any way to put all that in one query?

Last edited by drew22299; 06-30-2008 at 06:23 AM..
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 08:47 AM Re: Get the difference between current date and date stored in database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
hmm ?
I don't understand you...
What I wrote IS 1 query; but displayed with the resulting outpu in the mysql front-end.
There it is:
Code:
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS('1985-02-08 21:28:45')), '%y')AS age;
Obviously, you would change the
TO_DAYS('1985-02-08 21:28:45') part to a field who holds your date in your table
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-03-2008, 06:14 AM Re: Get the difference between current date and date stored in database
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Sorry I just realised it was one query I managed to get the query to work and it retrieves some data but it doesn't retrieve the difference in days:

PHP Code:
SELECT FROM action WHERE (TO_DAYSNOW( ) ) - TO_DAYSdueDate )>1
I want to get the difference in days between the dueDate stored in the action table and NOW() what does the > 1 do?

I then tried using this query which also worked but when I specify the table to get the date from there was an error so I probably put the SELECT * FROM table in the wrong place?

PHP Code:
SELECT FROM action (FROM_DAYS(TO_DAYS(NOW())-TO_DAYS('dueDate')), '%d')AS DayDiff
Also, I want it to display the difference in days even if it isn't accurate because it will only be used for a rough estimate.

Thanks,

Last edited by drew22299; 07-07-2008 at 06:12 AM..
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 07-07-2008, 06:13 AM Re: Get the difference between current date and date stored in database
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Anyone have any ideas?
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 07-07-2008, 06:50 AM Re: Get the difference between current date and date stored in database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
sorry, I had missed your last post...
You don't want a select *, the day diff. IS waht you want, so put it into the select clause, not the from clause.
Try something like that:
Code:
SELECT (FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(action.dueDate)), '%d') AS DayDiff FROM action ;
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-08-2008, 08:13 AM Re: Get the difference between current date and date stored in database
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
I changed the query to the following and it seems to work

PHP Code:
SELECT (TO_DAYSdueDate ) - TO_DAYSNOW() ) ) AS dayDiff FROM action WHERE NOW() < dueDate 
It returns the field dayDiff and it appears to be calculating the difference in days from the current date and dueDate in the database. I'm trying to make a query that can work out how many days there are left until the due date so I put WHERE NOW() < dueDate

Thanks,

Last edited by drew22299; 07-08-2008 at 08:15 AM..
drew22299 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Get the difference between current date and date stored in database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30724 seconds with 12 queries