Help with Date script – PHP

  php

Q(Question):

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:

$query = "SELECT DATE_FORMAT(submitted, '%m/%d/%y') AS date FROM ads WHERE id='$id'";
$result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$date = stripslashes($row['date']);
}
$originaldate = $date; //any properly formated date
$ageindays = number_format(floor((date("U")-strtotime($originaldate))/86400),0);
//print $ageindays . " days";
$torenew = 30 - $ageindays;
echo "
Date Posted: &nbsp;$date
<br><br>
Ads remain active for 30 days.
<br><br>
";
if ($torenew <= '0')
{
echo "<span style='color:red'><strong>Ad Expired</strong></span>";
}
else
{
echo "You will need to renew this ad in <span style='color:red'><strong>$torenew</strong></span> days.";
}

OK, it works. But it triggers this error message:

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:

$ageindays = number_format(floor(date_default_timezone_set('America/Indianapolis')-strtotime($originaldate))/86400),0);

But it gave the error:

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

A(Answer):

Heya, DavidPr.

Call date_default_timezone_set() separately; it does not return what you are expecting (PHP: date_default_timezone_set – Manual).

A(Answer):

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?

A(Answer):

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.

@DavidPr

A(Answer):

Here’s the code that emails me any and all error messages:

<?php # Script 13.3 - config.inc.php
// This script determines how errors are handled.
// Flag variable for site status:
$live = TRUE;
// Error log email address:
$email = 'me@mysite.com;
// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars)
{
global $live, $email;
// Build the error message.
$message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
// Add the date and time.
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
// Append $e_vars to the $message.
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
if ($live)
{ // Don't show the specific error.
error_log ($message, 1, $email); // Send email.
// Only print an error message if the error isn't a notice.
if ($e_number != E_NOTICE)
{
echo '<div id="Error">A system error occurred. We apologize for the inconvenience.</div><br />';
}
}
else
{ // Development (print the error).
echo '<div id="Error">' . $message . '</div><br />';
}
} // End of my_error_handler() definition.
// Use my error handler.
set_error_handler ('my_error_handler');
?>

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):

date_default_timezone_set('America/Indianapolis')
$query = "SELECT DATE_FORMAT(submitted, '%m/%d/%y') AS date FROM ads WHERE id='$id'";
$result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$date = stripslashes($row['date']);
}
$originaldate = $date; //any properly formated date
$ageindays = number_format(floor((date("U")-strtotime($originaldate))/86400),0);
//print $ageindays . " days";
$torenew = 30 - $ageindays;
echo "
Date Posted: &nbsp;$date
<br><br>
Ads remain active for 30 days.
<br><br>
";
if ($torenew <= '0')
{
echo "<span style='color:red'><strong>Ad Expired</strong></span>";
}
else
{
echo "You will need to renew this ad in <span style='color:red'><strong>$torenew</strong></span> days.";
}

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.

A(Answer):

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

A(Answer):

Hey Dan,

Yes it fixed the problem! Thanks for your help.

LEAVE A COMMENT