PHP date format problem - please help

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

<?php echo date('Y'); ?>

Second one

[code]// 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 />";
        }

[/code]

Third one

[code]//$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 “new
”;
}
// is it newer than 3 weeks?
elseif ($pd_date > $compareDate3)
{echo “new
”;
}
// is it newer than 2 weeks?
elseif ($pd_date > $compareDate2)
{echo “new
”;
}
// is it newer than 1 week?
elseif ( $pd_date > $compareDate1)
{echo “new
”;
}
[/code]

Thank you for looking.

Regards,
Cerio

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 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. I’m hoping someone can tell exactly what that code above should be changed to in order to make it work, as my attempts to change it were all a disaster.

Regards,
Miss Cerio

All I can tell you is the date() function works fine for me but I am only running 5.2.14

This would be useful to know as I am sure my hosts will upgrade it eventually and I don’t want a script I am writing right now to break.

Anyone know whats up?

I think you do this though:

[php]date_default_timezone_set(‘America/Los_Angeles’);[/php]

add that at the top of all your pages… click here for your timezone http://www.php.net/manual/en/timezones.php

I don’t have a solution for the actual php pages, I believe they need a little bit of code about date format aded to them but I don’t know exactly what or where I would add it to the PHP on the files using date code BUT I have been given a solution, as long as your webhosts allow your site to have an .htaccess file, which they should.

Right at the top of the .htaccess file, put this line

php_value date.timezone America/Los_Angeles

(change America/Los_Angeles to your time zone, mine is Europe/London) and when the site is opened, the .htaccess file is the first thing checked, so the server is happy with the PHP files after that.
The date code in my PHP files now works fine again without it having been changed at all.

It has solved the problem for me and seems to be a good way of dealing with it.

Miss Cerio

That should have been ‘added’, not ‘aded’. I can’t see a way to edit my post

Cerio

I got a solution.
Open the htaccess file and just add this line at the top…
php_value date.timezone America/Los_Angeles
but change America/Los_Angeles to your time zone (I put Europe/London) and then the php files all work again without any date code having to be changed.

I assume you can add something to the php files themselves instead but, not knowing how to, the solution above worked fine for me, as the .htaccess file is checked before any other site files, so deals with it.

Cerio

Sponsor our Newsletter | Privacy Policy | Terms of Service