Jump to content

Calculating multiple lengths of time between dates


soycharliente

Recommended Posts

This code is giving me the exact same thing for both sections of the echo. Can anyone help me figure out why I'm not getting what I want? I'm trying to figure out the biggest length of time between 2 dates and the second biggest amount of time between 2 dates.

 

$r['thedate'] is obviously from the DB. It's a DATE field from the MySQL table.

 

<?php
$sql = "SELECT `thedate` FROM `lunch_data` dee
		JOIN `lunch_locations` el ON el.`id`=dee.`pid`
		WHERE el.`loc`='Carver\'s'
		ORDER BY `thedate` ASC";
$result = mysql_query($sql) OR DIE ("/Stats Error #10.");
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
$largest = 0;
$second_largest = 0;
$previous = 0;
$first_dates = array(1=>'',2=>'');
$second_dates = array(1=>'',2=>'');
while ($r = mysql_fetch_array($result))
{
	$thedate = $r['thedate'];
	if ($previous != 0)
	{
		$length = floor((date('U',strtotime($thedate)) - date('U',strtotime($previous))) / (60*60*24));
		if ($length > $second_largest)
		{
			$second_dates[1] = $previous;
			$second_dates[2] = $thedate;
			$second_largest = $length;
		}
		if ($length > $largest)
		{
			$first_dates[1] = $previous;
			$first_dates[2] = $thedate;
			$largest = $length;
		}
	}
	$previous = $thedate;
}
echo date('d F Y',strtotime($first_dates[1])).' - '.date('d F Y',strtotime($first_dates[2])).'<br />'.$largest.' days<br />';
echo date('d F Y',strtotime($second_dates[1])).' - '.date('d F Y',strtotime($second_dates[2])).'<br />'.$second_largest.' days';
}
else { echo "There should be numbers here. Oh well."; }
?>

Ok, I think I made it do what I wanted by changing the second if statement in the middle to an else if, but now the data is coming out backwards. IE: The "largest" is actually giving me the "second largest" and vice versa. I mean, as a solution, I could just switch the variables, but why is it assigning them like that?

 

<?php
$sql = "SELECT `thedate` FROM `lunch_data` dee
		JOIN `lunch_locations` el ON el.`id`=dee.`pid`
		WHERE el.`loc`='Carver\'s'
		ORDER BY `thedate` ASC";
$result = mysql_query($sql) OR DIE ("/Stats Error #10.");
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
$largest = 0;
$second_largest = 0;
$previous = 0;
$first_dates = array(1=>'',2=>'');
$second_dates = array(1=>'',2=>'');
while ($r = mysql_fetch_array($result))
{
	$thedate = $r['thedate'];
	if ($previous != 0)
	{
		$length = floor((date('U',strtotime($thedate)) - date('U',strtotime($previous))) / (60*60*24));
		if ($length > $second_largest)
		{
			$second_dates[1] = $previous;
			$second_dates[2] = $thedate;
			$second_largest = $length;
		}
		else if ($length > $largest)
		{
			$first_dates[1] = $previous;
			$first_dates[2] = $thedate;
			$largest = $length;
		}
	}
	$previous = $thedate;
}
echo date('d F Y',strtotime($first_dates[1])).' - '.date('d F Y',strtotime($first_dates[2])).'<br />'.$largest.' days<br />';
echo date('d F Y',strtotime($second_dates[1])).' - '.date('d F Y',strtotime($second_dates[2])).'<br />'.$second_largest.' days';
}
else { echo "There should be numbers here. Oh well."; }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.