shuffweb Posted April 11, 2008 Share Posted April 11, 2008 Hi everyone, im after a bit of help on the simplest of PHP/MySQL problems. Scenario: I am creating a get_week_number() function which established the current date and stores the value in the $_SESSION['current_date'] variable, I then initiate a query which compares this value to the start_date and end_date fields in a table named 'week' to establish the current week number. <?php session_start(); require_once ('./includes/db_connect.php'); $current_date = date("Y-m-d"); $_SESSION['current_date'] = $current_date; if (isset($_SESSION['current_date'])){ $query = "SELECT week.week_id FROM week WHERE '{$_SESSION['current_date']}' >= week.start_date AND < week.end_date "; $result = mysql_query($query); // Return a record, if applicable. $row = mysql_fetch_array ($result, MYSQL_NUM); if ($row) { $_SESSION['week_number'] = $row[0]; echo ("The week_number is " .$_SESSION['week_number']); } else{ echo ("The Week is $current_date"); } } ?> Problem : There are no errors on the page, however according to the table the week number should be 28 yet the week number that the query retrieves is always week number 26. Im not sure what to do?? Any help would be highly appreciated, and feel free to give me a clip round the ear, for missing something that I can imagine is going to be easy to solve. Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/ Share on other sites More sharing options...
rhodesa Posted April 11, 2008 Share Posted April 11, 2008 First thing I notice is the WHERE clause...it is a little off I think...try this: SELECT week.week_id FROM week WHERE '{$_SESSION['current_date']}' >= week.start_date AND '{$_SESSION['current_date']}' < week.end_date or better yet, try: SELECT week.week_id FROM week WHERE '{$_SESSION['current_date']}' BETWEEN week.start_date AND week.end_date Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/#findComment-515042 Share on other sites More sharing options...
craygo Posted April 11, 2008 Share Posted April 11, 2008 Any reason why you are querying a database to get a week number. php has the function built in $current_date = date("Y-m-d"); $week_num = date("W", strtotime($current_date)); echo $week_num; Ray Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/#findComment-515053 Share on other sites More sharing options...
Barand Posted April 11, 2008 Share Posted April 11, 2008 A couple of possible reasons come to mind 1 ) Week 1 could be the first week in April (financial year week 1) 2 ) Week 1 could be, say, 3 years ago, and he's now in week 160 Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/#findComment-515082 Share on other sites More sharing options...
craygo Posted April 11, 2008 Share Posted April 11, 2008 You know Barand, just cause you got 13000 post..... I know what you mean, my company's fiscal year starts first week of July Ray Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/#findComment-515085 Share on other sites More sharing options...
shuffweb Posted April 11, 2008 Author Share Posted April 11, 2008 Cheers for the replies guys, The system I am creating is an attendance register for my university, Therfore the week numbers are respective of an acedemic year. rhodesa - Cheers for your suggests, the second one proved succesful, your a star. Many Thanks - Dan Link to comment https://forums.phpfreaks.com/topic/100703-retrieving-a-week-number-help-needed/#findComment-515120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.