jacko_162 Posted September 19, 2011 Share Posted September 19, 2011 i have the below code which i wrote this monring, for some reason when i assign $date to fetch data from my database i just get the echo of "You had an Error" yet if i manually assign $date as the following it works: $date = strtotime("2011-09-20"); i have it set exactly the same in the database cus if i echo $date i get: 2011-09-20 whats going on? date in php is a cruel function to play around with.. here is my code: <?php // Query the DB to fetch all data where member_id matches that set in session $reminder = mysql_query("SELECT * FROM `reminders` WHERE member_id=$_SESSION[sESS_MEMBER_ID]") or die(mysql_error()); //While Loop the results while($fetch = mysql_fetch_array( $reminder )) { // Start date (TODAY!) $s_date = strtotime("now"); // End Date (+7 days from today) $e_date = strtotime("+1 week"); // Fetches Date stamp from "reminderDate" row in DB $date = $fetch['reminderDate']; // Checks if the date is between 2 ranges set above if($date > $s_date && $date < $e_date) //Echo The Results { echo "<div class='notification error png_bg'><a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a><div>Custom Error Notification BOX!</div></div>"; } else { echo "You had an Error"; //echo $date; // TEST that date outputs datestamp "2011-09-02" } //END LOOP } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/ Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 strtotime() returns an int timestamp... while your db field is in string format.. you will need to convert the fetched data into an int Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270632 Share on other sites More sharing options...
jacko_162 Posted September 19, 2011 Author Share Posted September 19, 2011 my database row is stored as "DATE" i didnt think that was a string? Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270650 Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 anything retrieved from a database will be in string format.. will need to be an int Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270654 Share on other sites More sharing options...
jacko_162 Posted September 19, 2011 Author Share Posted September 19, 2011 ok where do i need to look in the manual on changing the variable to an int.. i only found something on idate() Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270660 Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 you can also use mktime to make the future timestamp.. use that timestamp with the date function to get your date as a string.. you can also use date to get your current date as a string and compare them to your db table data.. Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270664 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2011 Share Posted September 19, 2011 Since you have a DATE data type, why don't you just write a query to match the rows that you want? Mysql has a couple dozen date functions that make using dates easy. Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270666 Share on other sites More sharing options...
AyKay47 Posted September 19, 2011 Share Posted September 19, 2011 Since you have a DATE data type, why don't you just write a query to match the rows that you want? Mysql has a couple dozen date functions that make using dates easy. as PFM suggested, perhaps a mysql solution here would be much easier.. refer here for a reference of mysql's date/time functions Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270667 Share on other sites More sharing options...
jacko_162 Posted September 19, 2011 Author Share Posted September 19, 2011 so i can specify in the mysql query by using "WHERE" date is between todays date and +7 days? Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270669 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2011 Share Posted September 19, 2011 SELECT * FROM `reminders` WHERE member_id={$_SESSION['SESS_MEMBER_ID']} AND reminderDate BETWEEN CURDATE() AND CURDATE() + INTERVAL 7 DAY Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270670 Share on other sites More sharing options...
jacko_162 Posted September 19, 2011 Author Share Posted September 19, 2011 i see what you did there indeed much easier way to limit results using the query. marked as solved. thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/#findComment-1270673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.