Jump to content


Photo

Why isn't it working?


  • Please log in to reply
36 replies to this topic

#21 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 18 September 2006 - 11:50 PM

Does anyone know why it's being so weird?
Schroedinger's Cat walks into a bar.
And doesn't.

#22 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 12:08 AM

Copy only the code below, no editing, no extra stuff at all. Save it as test.php. Upload it to your server and run it. Note the results you see.

<?php
$lastDatefed = "2006-09-17 20:44:03"; // your database value
$then = strtotime($lastDatefed); // in seconds since the start of time = 1158540243
echo "Last Date Fed is ". $then. " seconds<br/>"; // outputs 

$right_now = time(); // in seconds since the start of time varies depending on when you test
echo "Right now is ". $right_now. " seconds<br/>"; // outputs 

$timesincefed = $right_now - $then;
echo "# seconds since fed is ". $timesincefed. " seconds<br/>";

$dayssincefed = number_format($timesincefed/86400,2);
echo "Date last fed ". $lastDatefed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $dayssincefed. " days";
?>

Exactly the same code is running at http://www.halfadot.com/temp/test.php

Run that. Tell me if you get different results on your server (the times will be slightly different because we're counting seconds).
Legend has it that reading the manual never killed anyone.
My site

#23 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 12:20 AM

Last Date Fed is 1158540243 seconds
Right now is 1158625156 seconds
# seconds since fed is 84913 seconds
Date last fed 2006-09-17 20:44:03
Date right now 2006-09-18 20:19:16
And the difference is 0.98 days
Schroedinger's Cat walks into a bar.
And doesn't.

#24 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 12:37 AM

OK, there's the proof that my code works on your server. If you can't duplicate the results with a similar calculation applied in your script, it means either that you have mis-named variables, mis-placed logic, or what you think is in the database isn't what's really in it.  As always, echo out variables, echo out what comes from the database, echo out anything and everything to help you track down your problem.
Legend has it that reading the manual never killed anyone.
My site

#25 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 12:40 AM

I've looked over it again and again and it looks right, but it's not working  :-\
Schroedinger's Cat walks into a bar.
And doesn't.

#26 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 12:54 AM

I think the round script messes it up, now it says

Last Date Fed is 1158540243 seconds
Right now is 1158626813 seconds
# seconds since fed is 86570 seconds
Date last fed 2006-09-17 20:44:03
Date right now 2006-09-18 20:46:53
And the difference is 0 days

the difference should be 1 day because it was .9
Schroedinger's Cat walks into a bar.
And doesn't.

#27 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 12:59 AM

I think the round script messes it up, now it says

Date last fed 2006-09-17 20:44:03
Date right now 2006-09-18 20:46:53
And the difference is 0 days

the difference should be 1 day because it was .9


No, it should be 1 because - as you can see from the dates and times - more than 24 hours have passed.
Legend has it that reading the manual never killed anyone.
My site

#28 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 01:01 AM

That's what I'm saying, it should be 1 but it says 0.

Do you know why it's doing that?
Schroedinger's Cat walks into a bar.
And doesn't.

#29 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 01:37 AM

I got it to round but now it's not working at all, it just says that the lastdatefed was today when it's the 15th.

I modified the test code to include the lastdatefed from the database. Here's what I have:

<?php

include ("secure/config.php");
include ("secure/dbinfo.php");

$query = "SELECT * FROM chibifriendspets WHERE monopetID = '1'";
    $result = mysql_query($query ,$connection);
	$query_data = mysql_fetch_array($result);
	$lastDatefed = $query_data['lastDatefed'];

$lastDatefed = date("Y-m-d H:i:s",strtotime("$lasteDatefed")); // your database value
$then = strtotime($lastDatefed); // in seconds since the start of time = 1158540243
echo "Last Date Fed is ". $then. " seconds<br/>"; // outputs 

$right_now = time(); // in seconds since the start of time varies depending on when you test
echo "Right now is ". $right_now. " seconds<br/>"; // outputs 

$timesincefed = $right_now - $then;
echo "# seconds since fed is ". $timesincefed. " seconds<br/>";

$dayssincefed = number_format($timesincefed/86400,2);

$days = round($dayssincefed);

echo "Date last fed ". $lastDatefed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $days. " days";


?>

Schroedinger's Cat walks into a bar.
And doesn't.

#30 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 01:58 AM

do you see anything wrong with my code?
Schroedinger's Cat walks into a bar.
And doesn't.

#31 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 02:25 AM

This works:

<?php
include ("secure/config.php");
include ("secure/dbinfo.php");

$query = "SELECT * FROM chibifriendspets WHERE monopetID = '1'";
    $result = mysql_query($query ,$connection);
	$query_data = mysql_fetch_array($result);
	$lastDatefed = $query_data['lastDatefed'];

// remove this when you finish testing
$lastDatefed = "2006-09-15 11:00:00"; 
	
$then = strtotime($lastDatefed);
$right_now = time();
$timesincefed = $right_now - $then;
$dayssincefed = number_format($timesincefed/86400,2);
$days = round($dayssincefed);

// remove this when you finish testing
echo "Date last fed ". $lastDatefed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $days. " days";
?>

Then try removing the line where I force the lastDatefed to 2006-09-15 11:00:00 and run it again.  If it doesn't produce 3 days then the date in your database isn't what you think.
Legend has it that reading the manual never killed anyone.
My site

#32 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 02:51 AM

It produces

Date last fed
Date right now 2006-09-18 22:49:50
And the difference is 1 days

But I'm POSITIVE that the lastdatefed for the pet with an ID of 1 is 2006-09-15 10:30:20. I just copied that from the database so I know it's right.
Schroedinger's Cat walks into a bar.
And doesn't.

#33 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 11:09 AM

If that's what it produces, then what you think about the database can't be right, can it?

Are you sure that:

#1 - all of the current scripts have been uploaded to your server in the right folders
#2 - your current scripts are using the same database and same table name as you want them to
#3 - the database value for the record you're sure you're reading is correct

If the answer to all of those is yes, then it's illogical, irrational, inexplicable, and I give up.  I can't see how a database value generates one result and declaring the same variable with the same value as you claim is in the database can come up with a different result.


Legend has it that reading the manual never killed anyone.
My site

#34 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 09:29 PM

The problem is obviously that it's not recieving the variable correctly. Is there an error in my SQL query or something?
Schroedinger's Cat walks into a bar.
And doesn't.

#35 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 19 September 2006 - 09:49 PM

I got it to work. i simply added "as lastDatefed" to my query, the variable wasn't declared.
Schroedinger's Cat walks into a bar.
And doesn't.

#36 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 11:32 PM

I'm pleased you got it to work  ;D

I'm sure you've learned a lot in this exercise and the one before.  Good luck with your future scripting endeavours.
Legend has it that reading the manual never killed anyone.
My site

#37 Pi_Mastuh

Pi_Mastuh
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationMN

Posted 20 September 2006 - 12:04 AM

Thank you for all your help  ;D. I love these forums. Last month I would have looked at these scripts and said "huh...?" and now I actualy know what they mean and how to write them and everything  ;D
Schroedinger's Cat walks into a bar.
And doesn't.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users