knox203 Posted April 22, 2008 Share Posted April 22, 2008 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 https://forums.phpfreaks.com/topic/102250-solved-selecting-first-and-last-date-in-query/ Share on other sites More sharing options...
Northern Flame Posted April 22, 2008 Share Posted April 22, 2008 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 https://forums.phpfreaks.com/topic/102250-solved-selecting-first-and-last-date-in-query/#findComment-523673 Share on other sites More sharing options...
knox203 Posted April 22, 2008 Author Share Posted April 22, 2008 Your code led me through a couple modifications to the perfect solution. Thanks a ton, Northern Flame!! Link to comment https://forums.phpfreaks.com/topic/102250-solved-selecting-first-and-last-date-in-query/#findComment-523709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.