Page 1 of 1
Date question
Posted: Fri Oct 16, 2009 4:07 am
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
Re: Date question
Posted: Fri Oct 16, 2009 4:17 am
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
Re: Date question
Posted: Fri Oct 16, 2009 5:10 am
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
Re: Date question
Posted: Fri Oct 16, 2009 5:58 am
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
Re: Date question
Posted: Fri Oct 16, 2009 6:06 am
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.