Jump to content

[SOLVED] Selecting first and last date in query


knox203

Recommended Posts

Hello all! I'm wondering a good way to grab the first and last day out of a query. I need to pass these on to my fpdf app to supply a date range. I.E. - I have 36 rows returned ranging from 2008-03-01 - 2008-03-31, I would want to grab 2008-03-01 and 2008-03-31 as two separate values. Here's the code I'm working with:

<?
$ic = "select 
	id, 
	driver, 
	date, 
	job, 
	customer, 
	pay_period, 
	round(base,2) as base_round, 
	round(paid,2) as paid_round 
FROM 
	test.commission_report 
WHERE 
	driver = '". $_POST['username']. "' 
	and SUBSTRING(date, 6, 2) = '". $_POST['month']. "' 
	and pay_period = '". $_POST['payperiod']. "'
group by 
	id, 
	SUBSTRING(date, 6, 2)";
echo "<!-- $ic -->\r\n";
$ic_result = mysql_query($ic) or die (mysql_error());
$fields = mysql_fetch_assoc($ic_result);
$paid_total = array();
$base_total = array();

	$row_counter = 1; 
do
	{ 
	$paid_total[] = $fields['paid_round'];
	$base_total[] = $fields['base_round'];
	$newdate = $fields['first_date'];
	$driver = ereg_replace("99", "C", $fields['driver']);
	$row_counter++;
?>

you could run 2 queries like this:

 

<?php
$first = mysql_query("SELECT date FROM table ORDER BY date ASC LIMIT 1")or die(mysql_error());
$second = mysql_query("SELECT date FROM table ORDER BY date DESC LIMIT 1")or die(mysql_error());

$first_date = mysql_fetch_object($first);
$second_date = mysql_fetch_object($second);

$date_one = $first_date->date;
$date_two = $second_date->date;

echo "$date_one - $date_two";

?>

 

 

EDIT: oops, I left something out in the code,

try it now

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.