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++;
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.