adriscoll Posted June 2, 2011 Share Posted June 2, 2011 Hello. I am having difficult combining 2 functions. On their own, each works fine. I would like to sum all of the hours within a certain event type based on the current year. Year = in table as DATE type and formatted as 2011-05-01. My current code to sum the hours is: <?php include('dbconfig.php'); // Make a MySQL Connection mysql_connect("localhost", "$user", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $result = mysql_query("SELECT sum(hours) AS total_hours FROM event_schedule Where park = 'ParkA' ") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo "Hours Worked: "; echo $row['total_hours']; } ?> I was thinking of combining that with something similar to what's below but don't know how to. <?php $cur_year = date("Y"); echo $cur_year; ?> Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/238252-how-to-pull-yyyy-from-date-sum-of-hours/ Share on other sites More sharing options...
Pikachu2000 Posted June 2, 2011 Share Posted June 2, 2011 If I understand you correctly . . . SELECT SUM(hours) AS total_hours FROM event_schedule Where park = 'ParkA' AND YEAR(`your_date_field`) = YEAR(CURDATE())" Link to comment https://forums.phpfreaks.com/topic/238252-how-to-pull-yyyy-from-date-sum-of-hours/#findComment-1224385 Share on other sites More sharing options...
requinix Posted June 2, 2011 Share Posted June 2, 2011 I'm not sure of exactly how MySQL deals with date fields, but a AND `your_date_field` BETWEEN "2011-01-01" AND "2011-12-31" might be faster. Subject to various factors. Link to comment https://forums.phpfreaks.com/topic/238252-how-to-pull-yyyy-from-date-sum-of-hours/#findComment-1224388 Share on other sites More sharing options...
adriscoll Posted June 3, 2011 Author Share Posted June 3, 2011 Pikachu2000 , It worked like a charm. as always, thank you. Link to comment https://forums.phpfreaks.com/topic/238252-how-to-pull-yyyy-from-date-sum-of-hours/#findComment-1224443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.