Connecting Tech Pros Worldwide Help | Site Map

Help with Date script

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 3rd, 2008, 02:14 AM
Familiar Sight
 
Join Date: Mar 2007
Posts: 142
Default Help with Date script

I have a website that allows people to post ads. Ads remain active for 30 days. In their control panel they can manage their ads, i.e., Edit, Delete, Renew, etc.

In this case I'm working with the "Renew" feature. I want to show how many days they have left before their ad becomes inactive.

When they submit an ad the date is recorded in the database using MySQL's DATE function. (I'm not interested in the "time", just the date). Here's how I query the database and the code to do the above:

Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT DATE_FORMAT(submitted, '%m/%d/%y') AS date FROM ads WHERE id='$id'";
  2. $result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error: " . mysql_error());
  3. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  4. {
  5. $date = stripslashes($row['date']);
  6. }
  7.  
  8. $originaldate = $date; //any properly formated date 
  9. $ageindays = number_format(floor((date("U")-strtotime($originaldate))/86400),0);
  10.  
  11. //print $ageindays . " days";
  12.  
  13. $torenew = 30 - $ageindays;
  14.  
  15. echo "
  16. Date Posted: &nbsp;$date
  17. <br><br>
  18. Ads remain active for 30 days.
  19. <br><br>
  20. ";
  21.  
  22. if ($torenew <= '0')
  23. {
  24. echo "<span style='color:red'><strong>Ad Expired</strong></span>";
  25. }
  26. else
  27. {
  28. echo "You will need to renew this ad in <span style='color:red'><strong>$torenew</strong></span> days.";
  29. }
OK, it works. But it triggers this error message:
Expand|Select|Wrap|Line Numbers
  1. date(): It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead
I tried using this:
Expand|Select|Wrap|Line Numbers
  1. $ageindays = number_format(floor(date_default_timezone_set('America/Indianapolis')-strtotime($originaldate))/86400),0);
But it gave the error:
Expand|Select|Wrap|Line Numbers
  1. Parse error: syntax error, unexpected ',' in /blablabla/manage_ad.php on line 76 
That's the comma in the above just behind the - 86400)

How can I correct this code so that it works as intended and relieves me of these error messages?

Thanks
Reply
  #2  
Old December 3rd, 2008, 02:27 AM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 25
Posts: 5,435
Default

Heya, DavidPr.

Call date_default_timezone_set() separately; it does not return what you are expecting (PHP: date_default_timezone_set - Manual).
Reply
  #3  
Old December 3rd, 2008, 03:28 AM
Familiar Sight
 
Join Date: Mar 2007
Posts: 142
Default

Yes, I read that but I don't get it. First of all, I don't even understand the error message. My server uses php 5++ something and I no longer see errors in my code on the screen. Php error is turned off in the php.ini file for the server - I'm on a shared environment, so they're trying to protect everyone I guess.

Anyway, I'm having to use an error submitting script that I got from Larry Ullman's book. It emails the crap out of me on every little error. I get well over a hundred emails a day. It's killing me. I want to see messages if the code will not work so I can attempt to fix, but if the web page is working... then leave me alone.

I want my code to be right, but my skills won't always get me there. I don't understand most of this stuff. I can hack out simple basic stuff but much of this is well over my head. Maybe if I were 30 years younger...

I read those php manual pages and I see code that stretches from the ceiling to the floor. Just to caculate the number of days between two dates??? I don't know, there has to be a better way than this. Is there a javascript code that I could use instead?
Reply
  #4  
Old December 3rd, 2008, 05:25 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Age: 24
Posts: 885
Default

In reply, we can't see any code that emails things to you. It's your code, you must go through it to understand it.

As far as the time zone warning just call

date_default_timezone_set()

at the beginning of your file. What you pass to it must be of the following timezones:

PHP: List of Supported Timezones - Manual

Your original formula looked right (I haven't tested it) you can just easy print $ageInDays to see if its correct.



Quote:
Originally Posted by DavidPr View Post
Yes, I read that but I don't get it. First of all, I don't even understand the error message. My server uses php 5++ something and I no longer see errors in my code on the screen. Php error is turned off in the php.ini file for the server - I'm on a shared environment, so they're trying to protect everyone I guess.

Anyway, I'm having to use an error submitting script that I got from Larry Ullman's book. It emails the crap out of me on every little error. I get well over a hundred emails a day. It's killing me. I want to see messages if the code will not work so I can attempt to fix, but if the web page is working... then leave me alone.

I want my code to be right, but my skills won't always get me there. I don't understand most of this stuff. I can hack out simple basic stuff but much of this is well over my head. Maybe if I were 30 years younger...

I read those php manual pages and I see code that stretches from the ceiling to the floor. Just to caculate the number of days between two dates??? I don't know, there has to be a better way than this. Is there a javascript code that I could use instead?
Reply
  #5  
Old December 3rd, 2008, 06:38 AM
Familiar Sight
 
Join Date: Mar 2007
Posts: 142
Default

Here's the code that emails me any and all error messages:
Expand|Select|Wrap|Line Numbers
  1. <?php # Script 13.3 - config.inc.php
  2.  
  3. // This script determines how errors are handled.
  4.  
  5. // Flag variable for site status:
  6. $live = TRUE;
  7.  
  8. // Error log email address:
  9. $email = 'me@mysite.com;
  10.  
  11. // Create the error handler.
  12.  
  13. function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars)
  14. {
  15.  
  16. global $live, $email;
  17.  
  18. // Build the error message.
  19. $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
  20.  
  21. // Add the date and time.
  22. $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
  23.  
  24. // Append $e_vars to the $message.
  25. $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
  26.  
  27. if ($live)
  28. { // Don't show the specific error.
  29.  
  30. error_log ($message, 1, $email); // Send email.
  31.  
  32. // Only print an error message if the error isn't a notice.
  33.  
  34. if ($e_number != E_NOTICE)
  35. {
  36. echo '<div id="Error">A system error occurred. We apologize for the inconvenience.</div><br />';
  37. }
  38. }
  39. else
  40. { // Development (print the error).
  41.  
  42. echo '<div id="Error">' . $message . '</div><br />';
  43. }
  44.  
  45. } // End of my_error_handler() definition.
  46.  
  47. // Use my error handler.
  48.  
  49. set_error_handler ('my_error_handler');
  50. ?>
One thing I've noticed is that it will email me TWICE the same error message. This accounts for half of the emails I receive. I don't know why it does this.

Are you saying I just need to add "date_default_timezone_set()" in front of my code like this (I'm in Central US Time Zone):
Expand|Select|Wrap|Line Numbers
  1. date_default_timezone_set('America/Indianapolis')
  2.  
  3. $query = "SELECT DATE_FORMAT(submitted, '%m/%d/%y') AS date FROM ads WHERE id='$id'";
  4. $result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error: " . mysql_error());
  5. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  6. {
  7. $date = stripslashes($row['date']);
  8. }
  9.  
  10. $originaldate = $date; //any properly formated date 
  11. $ageindays = number_format(floor((date("U")-strtotime($originaldate))/86400),0);
  12.  
  13. //print $ageindays . " days";
  14.  
  15. $torenew = 30 - $ageindays;
  16.  
  17. echo "
  18. Date Posted: &nbsp;$date
  19. <br><br>
  20. Ads remain active for 30 days.
  21. <br><br>
  22. ";
  23.  
  24. if ($torenew <= '0')
  25. {
  26. echo "<span style='color:red'><strong>Ad Expired</strong></span>";
  27. }
  28. else
  29. {
  30. echo "You will need to renew this ad in <span style='color:red'><strong>$torenew</strong></span> days.";
  31. }
This code worked (without the "date_default_timezone_set()"), it just emailed me that error message (twice) telling me that is was not safe relying on my system's timezone setting. If this will fix that I'll be a happy man and I can then move on to the next problem.
Reply
  #6  
Old December 3rd, 2008, 06:45 AM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Age: 24
Posts: 885
Default

The code you posted has a custom error handler. That means if an error occurs PHP hands over the control to a function specified, in this case my_error_handler() function.

However this function does not contain a mail function. I'm not sure how this is getting email. I do see an email variable. perhaps change that to an invalid email, but it seems you have already done so. "me@mysite.com" is most likely not your email address.

As far as the date problem, That's exactly what i'm saying to do. Did it fix your problem? Please let us know here so that others can solve this same problem.

Thanks,





Dan
Reply
  #7  
Old December 3rd, 2008, 08:16 PM
Familiar Sight
 
Join Date: Mar 2007
Posts: 142
Default

Hey Dan,

Yes it fixed the problem! Thanks for your help.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.