General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 7th, 2004, 06:27 AM
marymary marymary is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Belgium
Posts: 5 marymary User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Wrong results when comparing dates [SOLVED]

Hi all, I'm kinda stuck on comparing dates.
I have these 3 selectboxes (one for day, one for month, one for year). Users can select a date which will then be compared to a date in my database (oracle/sql).

The date the user selected is put in a var like this :
$UserDate = date("d-m-y", mktime(0, 0, 0, $Month,$Day, $Year));

The date in the database is found through this query :
$q="Select TO_DATE(dt_mod, 'dd-mm-yy') as MOD from pval where...

When I echo these dates they both look ok but it if I compare them I get strange results.

This is the output :
dt_mod : 31-03-04 comes after UserDate : 20-02-04
------------
dt_mod : 05-04-04 comes before UserDate : 20-02-04
------------
dt_mod : 06-04-04 comes before UserDate : 20-02-04
------------
dt_mod : 06-04-04 comes before UserDate : 20-02-04

It seems as if only the days are compared and nothing more?
Anyone who can help me?

Reply With Quote
  #2  
Old April 7th, 2004, 09:40 AM
marymary marymary is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Belgium
Posts: 5 marymary User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Solution

Found it!

I worked now with mktime so I can count the number of seconds that passed and then you're only comparing numbers instead of dates

Code:
//Count number of seconds
$UserDate = mktime(0, 0, 0, $Month,$Day, $Year);

//Get the date in the right format
$UserDate2= date('d-m-y',mktime(0, 0, 0, $Month,$Day, $Year));

//Now we need to get the date out of the database as a string (not in a date format so no to_date)
$q="Select dt_mod as MOD from pval";

//Of course we need to get that date split in day-month-year so we can use mktime again to get the number of seconds
$day2= substr($row2[MOD],0,2);
$month2=substr($row2[MOD],3,2);
$year2=substr($row2[MOD],6,4);

//local settings can have influence on how the date looks
$DBDate=mktime(0,0,0,$month2,$day2,$year2);

//And now we can compare
if ($DBDate < $UserDate)
{
      echo "dt_mod : $row2[MOD] comes before UserDate2 :$UserDate2 <BR> ------------<BR>";
}
else 
{
      echo "dt_mod : $row2[MOD] comes after UserDate2 : $UserDate2  <BR>------------<BR>";
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Wrong results when comparing dates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT