Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Dates!!?? Pls Help 1

Status
Not open for further replies.

Forri

Programmer
Oct 29, 2003
479
MT
Hi All

I'm trying to compare 2 dates using:

Code:
$LEAVE_FIRSTDATE = date("d/m/Y",strtotime(MySQL_RESULT($SQL_Result, 0,"firstday_date")));	

$LEAVE_ENDDATE = explode ("/",$LEAVE_FIRSTDATE);
		
		$MNT  = $LEAVE_ENDDATE[1];
		$DAY  = $LEAVE_ENDDATE[0];
		$YEAR = $LEAVE_ENDDATE[2];
		
		$LEAVE_ENDDATE 		= date("d/m/Y",mktime (0,0,0,$MNT,$DAY + $LEAVE_DAYS, $YEAR));
		
		if ($LEAVE_APPROVED == 1) { $LEAVE_APPROVED = "Approved"; $L = 1;}
		elseif (strtotime(date("d/m/Y")) >= strtotime($LEAVE_ENDDATE)) {$LEAVE_APPROVED = "Not Approved"; $L = 2;}
		elseif (strtotime(date("d/m/Y")) <  strtotime($LEAVE_ENDDATE)) {$LEAVE_APPROVED = "Waiting"; $L = 0;}

The above always returns "Not Approved"!!

How can i compare dates better?

Thanks
nick
 
What I generally do when I have to compare dates in PHP is to convert all dates to their seconds-count equivalents and compare those integers.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
If I have to compare dates and theres a database involved, I make the database do the work.

I can't follow your logic tho



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Better way is comparing in the YMD format (without slashes)

if ($year.$month.$day < date("Ymd")){
$LEAVE = "WAITING";
else $LEAVE = "Not Approved";


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin@anikin-skywalker.com
 
I'm chaging them to their seconds equivelent but i can't understand why .....they are still not comparing corectly! Should i change the strings to numeric values? or should they still compare!?

Thanks
Nick
 
If you are formatting your date strings as YYYY-MM-DD or some equivalent (but in the order of year, then month, then date), then comparing two date strings will work as well as two date integers.

I'd start verifying the values in my date-holding variables to make sure they contain what I think they do.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks worked great! I tried to format them d-m-Y which did not work!

Great!

Cheers nick
 
Forri:

There is one thing that you may have already notices, but I'll point it out anyway.

When a date is in YYYY-MM-DD format, date order and alphabetical order are one and the same.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top