Jump to content

Checking between a range of 2 dates


jacko_162

Recommended Posts

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
}
?>

Link to comment
https://forums.phpfreaks.com/topic/247425-checking-between-a-range-of-2-dates/
Share on other sites

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..

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

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.