glennda Posted March 23, 2011 Posted March 23, 2011 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?
webman Posted March 23, 2011 Posted March 23, 2011 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; } 1
glennda Posted March 23, 2011 Author Posted March 23, 2011 I thought there might have been a better way to do it! Cheers
glennda Posted March 23, 2011 Author Posted March 23, 2011 (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 March 23, 2011 by glennda Correct my code!
webman Posted March 23, 2011 Posted March 23, 2011 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'].""; ?>
glennda Posted March 23, 2011 Author Posted March 23, 2011 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
glennda Posted March 23, 2011 Author Posted March 23, 2011 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now