FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Check the closest event in Calendar?





Diablosblizz
I was wondering how I could check the closest event to today's day. So if today was September 1st, and the next event was September 4th then it would say that September 4th was the closest event. My date entry is setup as DD/MM/YYYY. I'm not sure what else information you need, but if you need anything ask.

Thanks.
rvec
how do you store all dates? If you store them in a mysql database you should use the format YYYY-MM-DD with the date data type and you can use a query like this:
SELECT * FROM events WHERE date > CURDATE() ORDER BY date LIMIT 1;

same with datetime data type in mysql or timestamp data type. Other data types need some work in php. It would be possible with php but in mysql is far easier.
Diablosblizz
Okay, I've gotten that to work, although when I try to get the amount of days remaining until the event I get a negative number if the day is higher than the next event. I'm using this:

Code:
$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 0, 1");
$event = mysql_fetch_array($selectc);
$days = $event['day'];
$dayss = date("d");
$day = $days - $dayss;


Then I just echo $day. It gives me a negative number if $days is below the current date ($dayss). Anyway I can fix this?
rvec
you should only use the full date, not just the day. You can't use only a day to calculate the number of days till the next event.

If you use the DATE mysql data type you could use this in php:
Code:
$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 1");
$event = mysql_fetch_array($selectc);
$date = $event['event_date'];//get the event date

$format = '%Y/%m/%d';
$date = strptime($date, $format);//put it in an array

$e_stamp = mktime(0, 0, 0, $date['tm_mon']+1, $date['tm_mday'], $date['tm_year']+1900);//use the array to make a timestamp

$days = (time() - $e_stamp) / 60*60*24;//use the timestamp and the current time to calculate the number of days between them and round it up (2.5 days is 3 days)


LIMIT row_count is the same as LIMIT 0, row_count
Diablosblizz
Quote:
Fatal error: Call to undefined function strptime() in C:\xampp\htdocs\techcrew\body.php on line 31


Line 31 is

Code:
$date = strptime($date, $format);//put it in an array


I think it might be a version problem, but I am hosting myself so I don't think everything is default to webhosting standards. I also use this script to figure out the month as well, not just the date. Thanks for your help.
rvec
ah I see:
Quote:
Note: This function is not implemented on Windows platforms.

I could try to rewrite it tomorrow.
Diablosblizz
Blasted Windows Server! Sad Is there anything else that I could use that would use the same method and output the same, or close to, as this function?

Thanks rvec.
rvec
I made a function to convert the mysql DATE data type to a unix timestamp, I think that should solve the problem.
Code:
function get_timestamp($date) {
   $date_ar = explode('-', $date);
   
   $days = ($date_ar[0] - 1970) * 365;
   switch($date_ar[1]) {
      case 1:
         $days += 31;
      break;
      case 2:
         if ($date_ar[0] % 4 == 0) {
            $days += 29 + 31;
         } else {
            $days += 28 + 31;
         }
      break;
      case 3:
         $days += 31 + 28 + 31;
      break;
      case 4:
         $days += 30 + 31 + 28 + 31;
      break;
      case 5:
         $days += 31 + 30 + 31 + 28 + 31;
      break;
      case 6:
         $days += 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 7:
         $days += 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 8:
         $days += 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 9:
         $days += 31 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 10:
         $days += 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 11:
         $days += 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 12:
         $days += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
   }
   
   return $days * 24 * 60 * 60;
}

$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 1");
$event = mysql_fetch_array($selectc);
$e_stamp = get_timestamp($event['event_date']);//get the event date and convert it to timestamp
$days = (time() - $e_stamp) / 60*60*24;//use the timestamp and the current time to calculate the number of days between them and round it up (2.5 days is 3 days)
Diablosblizz
Quote:
The next closest event from today is Test. The event is months and -85378248 days away.


I get that now, although I believe I have my variables messed. I am using this so far:

Code:
$today = date("Y-m-d");
$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 0, 1");
$event = mysql_fetch_array($selectc);
$amount = mysql_num_rows($selectc);
$name = $event['name'];
$date = $event['day'];
function get_timestamp($date) {
   $date_ar = explode('-', $date);
   
   $days = ($date_ar[0] - 1970) * 365;
   switch($date_ar[1]) {
      case 1:
         $days += 31;
      break;
      case 2:
         if ($date_ar[0] % 4 == 0) {
            $days += 29 + 31;
         } else {
            $days += 28 + 31;
         }
      break;
      case 3:
         $days += 31 + 28 + 31;
      break;
      case 4:
         $days += 30 + 31 + 28 + 31;
      break;
      case 5:
         $days += 31 + 30 + 31 + 28 + 31;
      break;
      case 6:
         $days += 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 7:
         $days += 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 8:
         $days += 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 9:
         $days += 31 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 10:
         $days += 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 11:
         $days += 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 12:
         $days += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
   }
   
   return $days * 24 * 60 * 60;
}


$date returns 1, being the date of the next event, so I think that's what the problem is. What do I need to get this to work?
rvec
first thing:
function foo($var) {
}
is a function declaration, it is a way to make a function called 'foo' which accepts one variable that will be called $var in the function itself. It is the same as die() or var_dump().

By just declaring the function nothing happens, you have to call it. The return thing in the end is what it returns when you call it . You call the function 'foo' like this:
foo('something here');

And to catch the return value in $bar:
$bar = foo('somethin');

so first solution, declare the function at start and don't forget to call it. And you don't have to call the variable the function uses $date.

Second, the function wants a variable with the mysql DATE data type as input.

The query also wants that format which (if you want today) can be made with CURDATE() instead of making a variable in php (only in the mysql query).

third: LIMIT 0, 1 is the same as LIMIT 1 which will always return one result(or 0 if there are no results at all). So mysql_num_rows is always 1 (or 0 if there are no results at all).
Diablosblizz
I apologize, but I don't really understand. Instead of echoing days, which I did, I replaced it with $bar which is:

Code:
$bar = get_timestamp($event['event_date']);


I apologize if that's not correct, but now I get "1222041600" days away. I have no idea what you're talking about. I apologize.
rvec
that's the timestamp.

Why don't you just use the script I wrote and add some stuff to it?

The $bar was just an example, you can call it any way you like.

The function will turn the mysql date format into a unix timestamp (sec since jan 1, 1970).
time() gives the timestamp of this moment
timestamp returned by function - time() = sec till event
sec till event / 24*60*60 = days till event
Diablosblizz
I'm so lost. I am using the script you wrote. I still don't understand, if I am doing this properly then why am I getting so many numbers.

I apologize, I don't really understand what this function needs to display, and what should it display?
rvec
the function changes the date type which you get out of the query to a timestamp which you can easier use in php.

You should just copy thw whole function to the beginning of the script and don't look at it any more. Then you can use the function just like you would use any other function (like var_dump(), die(), mysql_query(), ....).
Diablosblizz
I am, yet I am getting those crazy numbers.
rvec
what script are you using now, paste it here and I'll rewrite it.
Diablosblizz
Code:
function get_timestamp($date) {
   $date_ar = explode('-', $date);
   
   $days = ($date_ar[0] - 1970) * 365;
   switch($date_ar[1]) {
      case 1:
         $days += 31;
      break;
      case 2:
         if ($date_ar[0] % 4 == 0) {
            $days += 29 + 31;
         } else {
            $days += 28 + 31;
         }
      break;
      case 3:
         $days += 31 + 28 + 31;
      break;
      case 4:
         $days += 30 + 31 + 28 + 31;
      break;
      case 5:
         $days += 31 + 30 + 31 + 28 + 31;
      break;
      case 6:
         $days += 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 7:
         $days += 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 8:
         $days += 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 9:
         $days += 31 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 10:
         $days += 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 11:
         $days += 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 12:
         $days += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
   }
   
   return $days * 24 * 60 * 60;
}

$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 1");
$event = mysql_fetch_array($selectc);
$e_stamp = get_timestamp($event['event_date']);//get the event date and convert it to timestamp
$days = (time() - $e_stamp) / 60*60*24;//use the timestamp and the current time to calculate the number of days between them and round it up (2.5 days is 3 days)
$amount = mysql_num_rows($selectc);
$name = $event['name'];


Your function and my selects at the top, while I echo this below:

Code:
$bar = get_timestamp($event['event_date']);
if($amount == 1) {            
echo "The next closest event from today is $name. The event is $month months and $bar days away.";
}


event_date is the MySQL data format (YYYY-MM-DD).
rvec
you forgot to set $today. Which you should only set if you want to be able to use other dates then today there, because instead of $today you could also use CURDATE()

Code:
$days = (time() - $e_stamp) / 60*60*24;

should be
Code:
$days = ($e_stamp - time()) / 60*60*24;

since $_stamp is in the future, so has to be bigger then time().
Also note that time() is always this moment, so if you use $today (in the query) to support other dates then today, you should also change this part to :
Code:
$n_stamp = get_timestamp($today);
$days = ($e_stamp - $n_stamp) / 60*60*24;


also don't forget to floor() the $days variable, else you'll get an event that is 3,97294 days away.
Code:
$days = floor($days);



Code:
$bar = get_timestamp($event['event_date']);

wtf, you just did everything right, and now you just throw it all away and do it the wrong way?
instead of the whole last part (including the $bar = part) try this:
Code:
if($amount == 1) {           
echo "The next closest event from today is $name. The event is $days days away.";
}


Months make it much harder, because you'll have to find out how big the months are (28, 31, 30) and if you have about 15 events a year I don't think you'll need the months a lot.
Diablosblizz
$today was set.

Code:
$today = date("Y-m-d");


And if it's set or not doesn't do anything to the code.
rvec
This should work:
Code:
function get_timestamp($date) {
   $date_ar = explode('-', $date);
   
   $days = ($date_ar[0] - 1970) * 365;
   switch($date_ar[1]) {
      case 1:
         $days += 31;
      break;
      case 2:
         if ($date_ar[0] % 4 == 0) {
            $days += 29 + 31;
         } else {
            $days += 28 + 31;
         }
      break;
      case 3:
         $days += 31 + 28 + 31;
      break;
      case 4:
         $days += 30 + 31 + 28 + 31;
      break;
      case 5:
         $days += 31 + 30 + 31 + 28 + 31;
      break;
      case 6:
         $days += 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 7:
         $days += 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 8:
         $days += 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 9:
         $days += 31 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 10:
         $days += 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 11:
         $days += 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
      case 12:
         $days += 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
      break;
   }
   
   return $days * 24 * 60 * 60;
}

$today = date("Y-m-d");
$selectc = mysql_query("SELECT * FROM `calender` WHERE `event_date` > '$today' ORDER BY `event_date` LIMIT 1");
$event = mysql_fetch_array($selectc);
$e_stamp = get_timestamp($event['event_date']);//get the event date and convert it to timestamp
$n_stamp = get_timestamp($today);
$days = ($e_stamp - $n_stamp) / (60*60*24);//use the timestamp and the current time to calculate the number of days between them and round it up (2.5 days is 3 days)
$days = floor($days);
$amount = mysql_num_rows($selectc);

if($amount == 1) {           
echo "The next closest event from today is {$event['name']}. The event is $days days away.";
}
Diablosblizz
This somewhat works, although the number is wrong. For example, if I have a event on the first of September and today is the 15th of August, then it would say 31 days till the next event, which is wrong. The code just counts how many days until next month.

So, from the 15th of August to the 15th of September (being 31 days).

Quote:
The next closest event from today is Commencement. The event is 31 days away.


If I change the month on my server, then do the same thing it does the exact same.
rvec
oh I forgot to add the days Embarassed
add this before the return row (last row of the function)
Code:
$days += $date_ar[2];
Diablosblizz
Yes, that worked perfectly! Thank you very much Rvec!!
themit
I like to have dates saved with the unix timestamp personally.

For example, you can enter all the date details, then create a timestamp.
Code:

$date=mktime(0,0,0,0,0,0);


Then you get an output like: 12849439 which represents the seconds after the Epoch. With that stored in a mysql database, you simply do this:
Code:

$query=mysql_query("SELECT*FROM dates ORDER BY `date` DESC");
while($array=mysql_fetch_array($query)){
if(!isset($event))&&$array['date']<date("U")){
$event=$array['id'];
}
}

In the above code, $event returns the event id with the date closest to today.
rvec
that's a bad way to do the same thing.

You should use this if you use the unix timestamp:
Code:
$now = time();
$query=mysql_query("SELECT*FROM dates WHERE date>$now ORDER BY `date` DESC" );

and why does everyone use ``, those give errors.
Related topics
Winxp SP2 tcpip.sys Patcher Version 2.12 released!
Calendar with PHP
System reboots itself...
My computer restarts?
Check if the client has JavaScript enabled
what plugin you used on your Wordpress
blue screen of death 0x8E
Fixing something in MyEvent 1.5
floating menu!
Can someone help with a problem
Problem with Windows Vista Drivers upgade.
What Religion are You?
Man To Go Cashless For A Year
Windows XP randomly locking up
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.