Page 1 of 1

It's not making sense much - mktime funtion problem - help

Posted: Sun Apr 22, 2007 6:47 pm
by crazytopu
Hi this is not making any sense to me, can anybody point out what am I missing here?

Code: Select all

<?
	
		$search_customer = "SELECT * FROM customerorder WHERE customerid = '".$_POST['cusid']."'"; 		
		//echo $search_customer;
		
		$result = $connector->query($search_customer);
		$num_rows = mysql_num_rows($result);

		
		
	
		if($num_rows>0){
		
		/** a variable that will determine the most recent date when the customer placed an order, intitally assigned to an old value */
		$most_recent_order_date = "2000-00-00";
		
		echo "<br>value before loop: ".$most_recent_order_date;
		while($row=$connector->fetchArray($result)){
		
		/** if more recent date is found, assign the new date to the most recent date variable*/
		if($row['dateof']>$most_recent_order_date){ 
		$most_recent_order_date = $row['dateof']; 
		}
			
		}
			
		}
		
		echo "<br>value after loop:".$most_recent_order_date;	
		
		/** converting the date to a format that we can use with mktime function in order to find otu the difference between today and the most recent order date*/
		$most_recent_order_date = date("m,d,Y",strtotime($most_recent_order_date));
		
		$today = date("m,d,Y");
		echo "<br>After converting the formate: ".$most_recent_order_date;
		echo "<br>Today :".$today;
		
		
		
	    /** find out how old the order is - if it is 3 months user is inactive, otherwise active*/
		
		/** some testing value that just works fine */
echo "<br>....................some dummy value ..........................................";					
		$dateDiff = mktime(0,0,0,04,24,2007) - mktime(0,0,0,04,22,2007);
echo '<br>Difference in seconds: ' . $dateDiff . '<br />';

echo '<br />Years Difference   = '. floor($dateDiff/365/60/60/24);
echo '<br />Months Difference  = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Weeks Difference   = '. floor($dateDiff/60/60/24/7);
echo '<br />Days Difference    = '. floor($dateDiff/60/60/24);
echo '<br />Hours Difference   = '. floor($dateDiff/60/60);
echo '<br />Minutes Difference = '. floor($dateDiff/60);

/** now some real data to play with, still no problem */
echo "<br>....................real data but still not a problem.........................................";					
		$dateDiff = mktime(0,0,0,$today) - mktime(0,0,0,04,22,2007);
echo '<br>Difference in seconds: ' . $dateDiff . '<br />';

echo '<br />Years Difference   = '. floor($dateDiff/365/60/60/24);
echo '<br />Months Difference  = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Weeks Difference   = '. floor($dateDiff/60/60/24/7);
echo '<br />Days Difference    = '. floor($dateDiff/60/60/24);
echo '<br />Hours Difference   = '. floor($dateDiff/60/60);
echo '<br />Minutes Difference = '. floor($dateDiff/60);

/** now some real data to play with, now problem! */
echo "<br>....................problem area ..........................................";				
		$dateDiff = mktime(0,0,0,$today) - mktime(0,0,0,$most_recent_order_date);
echo '<br>Difference in seconds: ' . $dateDiff . '<br />';

echo '<br />Years Difference   = '. floor($dateDiff/365/60/60/24);
echo '<br />Months Difference  = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Weeks Difference   = '. floor($dateDiff/60/60/24/7);
echo '<br />Days Difference    = '. floor($dateDiff/60/60/24);
echo '<br />Hours Difference   = '. floor($dateDiff/60/60);
echo '<br />Minutes Difference = '. floor($dateDiff/60);		
		
		?>

this gives the following output, it should show the difference as 1 but it is showing as 0

value before loop: 2000-00-00
value after loop:2007-04-22
After converting the formate: 04,22,2007
Today :04,23,2007
....................some dummy value ..........................................
Difference in seconds: 172800

Years Difference = 0
Months Difference = 0
Weeks Difference = 0
Days Difference = 2
Hours Difference = 48
Minutes Difference = 2880
....................real data but still not a problem.........................................
Difference in seconds: 86400

Years Difference = 0
Months Difference = 0
Weeks Difference = 0
Days Difference = 1
Hours Difference = 24
Minutes Difference = 1440
....................problem area ..........................................
Difference in seconds: 0

Years Difference = 0
Months Difference = 0
Weeks Difference = 0
Days Difference = 0
Hours Difference = 0
Minutes Difference = 0

Posted: Sun Apr 22, 2007 7:01 pm
by Weirdan
don't you see the difference between:

Code: Select all

$dateDiff = mktime(0,0,0,"04,23,2007") - mktime(0,0,0,"04,22,2007");
and

Code: Select all

$dateDiff = mktime(0,0,0,4,23,2007) - mktime(0,0,0,04,22,2007);
?

The first snippet is what you're actually doing in your code.

Posted: Sun Apr 22, 2007 7:08 pm
by crazytopu
Hey, thanks a lot!

I was confused because this

Code: Select all

$dateDiff = mktime(0,0,0,$today) - mktime(0,0,0,04,22,2007);
shows the value correctly. So, I thought $today and $most_recent_order_date variable outputs value similar way. But now you are saying he latter outputs as string. Can you show me how to get rid of this string quote?
I mean where do i have to make the change?

Many Thanks.

Posted: Sun Apr 22, 2007 8:16 pm
by John Cartwright
You need to create 3 variables, one for day, month and year and pass those variables appropriately to mktime