Click to See Complete Forum and Search --> : comparing dates


john noble
10-18-2007, 08:14 AM
In my code I would like to compare a date held in a MYSQL table with the current date.

The name of the field is 'dateAdded', the datatype for this field is DATE, format 0000:00:00

Here in the UK we tend to use dd/mm/yyyy format.

I want a row of table to be displayed in RED if the dateAdded value is < todays date.

something like:

if dateAdded < date()
{
// print in red
}
else
{
// print in black
}

Not sure how to go about this though.

John

stephan.gerlach
10-18-2007, 08:25 AM
if ($dateAdded <= date('Y:m:d') ) {

echo '<span style="color: #ff0000;">'.$dateAdded.'</span>';

}

else {

echo $dateAdded;

}

john noble
10-18-2007, 08:52 AM
Perfect
Thanks,

John

stephan.gerlach
10-18-2007, 09:30 AM
no problem

ShaReB
10-19-2007, 09:32 AM
stephan.gerlach, so what if $dateAdded is DateTime data type in the db?

and how to format the date from the db to be something like "September 12th, 2007"?

thanks in advance
~Shareb

stephan.gerlach
10-19-2007, 09:42 AM
something like this. probably wont work but i am sure you can figure it out from here



// split the datetime at the space
$parts = explode(' ',$dateAdded);

// split date at the :
$date_parts= explode(':',$parts[0]);

if ($parts[0] <= date('Y:m:d') ) {

echo '<span style="color: #ff0000;">'.date('F dS, Y',mkdate(1,1,1,$date_parts[1],$date_parts[2],$date_parts[0])).'</span>';

}

else {

echo $dateAdded;

}

ShaReB
10-19-2007, 01:18 PM
Thanks, i got the idea..