Jump to content

Recommended Posts

Posted

I'm currently trying to put the theme of the week on our new intranet - i'm slightly stuck with how best to do it.

 

I have a spreadsheet which has all the themes but its week commencing so all is dated monday rather then change it all for each day of the week with the correct theme i was thinking of doing this

 

 

$date =get date
if ($date = monday) {$theme =mysql query Selete * from table where week=$date echo $theme} elseif ($date = tuesday) { $theme =mysql query selete * from table where week =$date -1day echo $theme} elseif etc for wed thurs and friday.

 

Enless anybody can think of a better way of doing it?

Posted

Just find the Monday date of the week that you're currently in, and then query on that.

 

This function might help you. Give it today's date, and it will give you current/last Monday's date. All in yyyy-mm-dd format.

 

function get_monday($date){
// Find start of week of given date in format YYYY-MM-DD
$dateparts = explode('-', $date);
$thedate = mktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]);
if( date("w", $thedate) == 1 ){
	$monday = date("Y-m-d", $thedate);
} else {
	$monday = date("Y-m-d", strtotime("last Monday", $thedate));
}
return $monday;
}

  • Thanks 1
Posted (edited)

I've tried this

 

function get_monday($date){ 
   // Find start of week of given date in format YYYY-MM-DD 
   $dateparts = explode('-', $date); 
   $thedate = mktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); 
   if( date("w", $thedate) == 1 ){ 
       $monday = date("Y-m-d", $thedate); 
   } else { 
       $monday = date("Y-m-d", strtotime("last Monday", $thedate)); 
   } 
   return $monday; 
}  $theme = mysql_query("SELECT * FROM themes WHERE date = '$monday'");
$themes = mysql_fetch_array($theme);
                  echo "" .$themes['theme']."" ; ?>

 

But I just get undefined variable monday?

 

although it works with just the datestamp in the query

Edited by glennda
Correct my code!
Posted

You need to call the function get_monday() and give it a date (today's date). E.g.

 


// Define the function that gets the monday
function get_monday($date){ 
   // Find start of week of given date in format YYYY-MM-DD 
   $dateparts = explode('-', $date); 
   $thedate = mktime(0, 0, 0, $dateparts[1], $dateparts[2], $dateparts[0]); 
   if( date("w", $thedate) == 1 ){ 
       $monday = date("Y-m-d", $thedate); 
   } else { 
       $monday = date("Y-m-d", strtotime("last Monday", $thedate)); 
   } 
   return $monday; 
}

// Call the function and assign return value to a variable
$monday = get_monday(date('Y-m-d'));

// Run query
$theme = mysql_query("SELECT * FROM themes WHERE date = '$monday'");
$themes = mysql_fetch_array($theme);

echo "" .$themes['theme']."";
?>

Posted

Slightly off topic but i'm still trying to do the same thing!

 

do you know a way that i can import the date from the excel spreadsheet (or csv) in yyyy-mm-dd format into a timestamp? as currently when i try and import it i just get 0000-00-00 00:00:00

 

EDIT: i'm using phpmyadmin

Posted

Not to worry i've sorted

 

the reason it was strange before was the way the spreadsheet was holding the time data becuase the teacher had set the date in the yyyy-mm-dd format using excels format cell and it was displaying it as 4056 for example

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...