|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? ![]() |
|
#2
|
|||
|
|||
|
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>";
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Wrong results when comparing dates |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|