Date question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Date question

Post by Addos »

Code: Select all

if ('15/11/2009' <= date('d/m/Y')) 
{ 
echo 'overdue'; 
}
Can’t understand why the code seems to only return the correct value if only the ‘day’ is changed. If I change the month or year is doesn’t make any difference to the result.

Basically I wanted this to return ‘overdue’ if the full date is today or less than today.
Thanks for any tips
litarena
Forum Newbie
Posts: 14
Joined: Tue Sep 01, 2009 3:39 am

Re: Date question

Post by litarena »

You probably need to convert the date into a unix date timestamp and compare the time stamps. Read this: http://www.phpf1.com/tutorial/php-date-compare.html
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Date question

Post by Mark Baker »

It doesn't work because you're comparing strings, not dates.

Code: Select all

if (strtotime('15/11/2009') <= time()) { 
echo 'overdue'; 
}
But watch strtotime and your date format: you're using a UK format (15th day of the 11th month), which may error because if it was parsed as a US format (11th day of the 15th month) it would be an invalid date
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Date question

Post by Addos »

Thanks guys this is great help and I have been working on a combination of both replies.

I need the UK date format and I came up with the following.

Code: Select all

    // Need to convert from European date to USA First  
    function tousdate($date) {
    $d = explode('/', $date);
    $d = "$d[2]/$d[1]/$d[0]"; // returns YYYY/MM/DD
    return $d;
    }
    $usDate = tousdate($row_GetTotal['date_due']);
        if(strtotime($usDate) <= time()) { echo ' overdue'; }
This works to some extent. I have a do loop running with this inside it and it’s giving me the error Fatal error: Cannot redeclare tousdate() If I remove the loop I get the correct result but I obviously need the loop. I can’t move the function to outside the loop (I think) as it’s needed within this code. I’m not too sure why I can’t re-declare this but will keep looking.
Thanks again
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Date question

Post by Addos »

Ok I just realised that I can and must move the function outside the loop. Wasn't sure but can now see it only needs to be declared once.

Thanks to all again. Got this sorted.
Post Reply