PHP date format problem - please help

  • cerio
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: UK

Post 3+ Months Ago

Hello.
I have a website with php in it but I cannot write php at all and have long lost touch with the one person I knew who understood it.

It was fine until my webhost updated from PHP 5.1 to PHP 5.3.
After he did that, all pages using code referring to dates messed up completely. It seems the date format that worked in 5.1 does not work in 5.3 and needs written a different way

Can anyone, please, tell me how the 5.1 version of the three pieces of code below should be written so that they will work in 5.3? I have no idea what to do to them. At the moment all I could do is completely remove that code from the files to get the site to work again but I would like to put it back, if it can be changed to work in 5.3, because it is very useful.

I'm in UK, in case that is required for the new code.

Many thanks to anyone who can help.

First one

Code: [ Select ]
<?php echo date('Y'); ?>



Second one

Code: [ Select ]
// is item new on site?
     
      //$today=date("Y-m-d");
    $compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y")));
    // -28 = 4 weeks
    $compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y")));
    // -21 = 3 weeks
    $compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y")));
    // -14 = 2 weeks
    $compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
    // -7 = 1 weeks

    
    // is it newer than 4 weeks?
    
    if ($pd_date > $compareDate4)
      {
      echo "<br /><img src='images/new1.gif' alt='new' /><br />";
      }
    // is it newer than 3 week?
    elseif ( $pd_date > $compareDate3)
      {echo "<br /><img src='images/new2.gif' alt='new' /><br />";
      }
    // is it newer than 2 weeks?
    elseif ($pd_date > $compareDate2)
      {echo "<br /><img src='images/new3.gif' alt='new' /><br />";
      }
    // is it newer than 1 week?
    elseif ( $pd_date > $compareDate1)
      {echo "<br /><img src='images/new4.gif' alt='new' /><br />";
      }
  1. // is item new on site?
  2.      
  3.       //$today=date("Y-m-d");
  4.     $compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y")));
  5.     // -28 = 4 weeks
  6.     $compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y")));
  7.     // -21 = 3 weeks
  8.     $compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y")));
  9.     // -14 = 2 weeks
  10.     $compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
  11.     // -7 = 1 weeks
  12.     
  13.     // is it newer than 4 weeks?
  14.     
  15.     if ($pd_date > $compareDate4)
  16.       {
  17.       echo "<br /><img src='images/new1.gif' alt='new' /><br />";
  18.       }
  19.     // is it newer than 3 week?
  20.     elseif ( $pd_date > $compareDate3)
  21.       {echo "<br /><img src='images/new2.gif' alt='new' /><br />";
  22.       }
  23.     // is it newer than 2 weeks?
  24.     elseif ($pd_date > $compareDate2)
  25.       {echo "<br /><img src='images/new3.gif' alt='new' /><br />";
  26.       }
  27.     // is it newer than 1 week?
  28.     elseif ( $pd_date > $compareDate1)
  29.       {echo "<br /><img src='images/new4.gif' alt='new' /><br />";
  30.       }


Third one


Code: [ Select ]
//$today=date("Y-m-d");
$compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y"))); // -28 = 4 weeks
$compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y"))); // -21 = 3 weeks
$compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y"))); // -21 = 3 weeks
$compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y"))); // -21 = 3 weeks

// is it newer than 4 weeks?
if ($pd_date > $compareDate4)
  {echo "<img src='images/new1.gif' alt='new' /><br />";
  }
    // is it newer than 3 weeks?
elseif ($pd_date > $compareDate3)
  {echo "<img src='images/new2.gif' alt='new' /><br />";
  }
// is it newer than 2 weeks?  
elseif ($pd_date > $compareDate2)
  {echo "<img src='images/new3.gif' alt='new' /><br />";
  }
// is it newer than 1 week?  
elseif ( $pd_date > $compareDate1)
  {echo "<img src='images/new4.gif' alt='new' /><br />";
  }
  1. //$today=date("Y-m-d");
  2. $compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y"))); // -28 = 4 weeks
  3. $compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y"))); // -21 = 3 weeks
  4. $compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y"))); // -21 = 3 weeks
  5. $compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y"))); // -21 = 3 weeks
  6. // is it newer than 4 weeks?
  7. if ($pd_date > $compareDate4)
  8.   {echo "<img src='images/new1.gif' alt='new' /><br />";
  9.   }
  10.     // is it newer than 3 weeks?
  11. elseif ($pd_date > $compareDate3)
  12.   {echo "<img src='images/new2.gif' alt='new' /><br />";
  13.   }
  14. // is it newer than 2 weeks?  
  15. elseif ($pd_date > $compareDate2)
  16.   {echo "<img src='images/new3.gif' alt='new' /><br />";
  17.   }
  18. // is it newer than 1 week?  
  19. elseif ( $pd_date > $compareDate1)
  20.   {echo "<img src='images/new4.gif' alt='new' /><br />";
  21.   }



Thank you for looking.

Regards,
Cerio
  • cerio
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: UK

Post 3+ Months Ago

Sorry, I forgot to include the error message that I got with all pages with date code...
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.


I did some searching and tried changing it with stuff about (Europe/London) etc. by following examples I found but I just made things worse, or no better, since PHP is just gobbledegook to me and I have no idea at all what it means or how to make any kind of changes. Reading PHP is as clear as reading Klingon would be to me, which means it is complete gobbledegook.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

cerio wrote:
Reading PHP is as clear as reading Klingon would be to me, which means it is complete gobbledegook.


That got me to laugh a bit, thanks for the humor :)

At the beginning of your script, a few lines past the <?php part have you tried putting something along these lines in yet?:

PHP Code: [ Select ]
date_default_timezone_set('America/Los_Angeles');


To see if that resolves your issue?

The reason this problem starting happening since you upgraded PHP is because in the upgrade to PHP 5.3.0 they put this update:

Quote:
5.3.0 Now throws E_WARNING rather than E_STRICT.
Every call to a date/time function will generate a E_NOTICE if the timezone isn't valid, and/or a E_WARNING message if using the system settings or the TZ environment variable.


So your problem was most likely always there, but now it throws out warnings which is what you are seeing at this point.
  • cerio
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: UK

Post 3+ Months Ago

Hi.
Thanks for your help.

The pages should look like this

Image


but due to the date format thingy, they look like this


Image

unless I remove the code I mentioned in my initial post (parts 2 and 3 affect those pages, part 1 is just affects a date in the footer). As I said, the site was fine until my webhost updated his PHP to 5.3, then it objected to the date format in my site's files and turned the pages to what you see above.

That code is what adds those little 'New' icons to things just added to the site, it changes the icon to a different coloured one each week for 4 weeks, then stops displaying any icon. It's an easy way for people to spot what is newly or very recently added to the site. When I removed the code, the site displayed properly again but the icons were no longer displayed.

I'm not altogether sure what you mean about 'a few lines past the <?php part', as there are <?php parts all over the place in the three files that have code concerning dates, but I will try putting it into each file and see what happens.

In the meantime, I have just been told a way to circumvent the problem without touching the scary php files. I put...
php_value date.timezone Europe/London
right at the top of the .htaccess file and that has allowed me to put the code about dates back into the php files and have it function properly again, with the pages displaying properly and the icons back.

I would not have been so lucky if there had been no htaccess file or if it had required anything more difficult done to the htaccess file than merely pasting that line at the top, because the htaccess file is a thing of mystery to me too.

I will play with a copy of the three php files, trying out what you suggest and seeing if I can do it without making things worse, as I feel uneasy about having had to rely on the htaccess file to solve it and it would be good if the site could work even if there was no htaccess file there at all.

I do hope my webhost doesn't update anything else that causes me problems, as I don't have the skills to deal with them.

I keep thinking I should learn PHP but then I open my PHP/MySQL for Dummies book and glaze over in fear, feeling very disheartened that a book for dummies seems so brain meltingly difficult. Klingon would be much less daunting to learn, if somewhat less useful.

Thank you for your suggestion. It will be interesting to see if my attempts to do it succeed. If not, I can at least leave the htaccess file to solving it for now.

Regards,
Cerio
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9092
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

First I must say there is nothing wrong with using a .htaccess file, that is a very common thing to use. So if changing or adding something there resolved your issues, I would call it good.

As far as after the <?php part, usually just after the first one in a given file would be sufficient, but really I think the .htaccess solution is a better solution as it automatically gets applied globally to any php file that you have then.
  • cerio
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: UK

Post 3+ Months Ago

ah, ok, thank you. That reassures me.

I will try your suggestion, though, just to see if I can work out how to do it but it is good to know the htaccess file is a good enough way, since I don't hold much hope of changing the php files successfully.

I didn't have an htaccess file at all until fairly recently, when I started getting a lot of bandwidth used up that was not legitimate visitors to the site or search engines that were useful, so I had to learn just enough to be able to create an htaccess file to try to deal with that. It doesn't seem to have helped that problem, though, despite a huge list of exclusions on it. Still going way over my bandwidth limit by the middle of each month. Luckily my webhost isn't making me pay for more bandwidth.... so far

Regards,
Cerio

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.