Jump to content

Retrieving a week number - Help needed


shuffweb

Recommended Posts

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

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

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

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.