Red dwarf
Red dwarf

Reputation: 63

Comparing curent date with date from database

I need help with my script that is comparing current date with date that is entered in database, the thing that I wont to do is get alert message when the date from database pass the current date, and alert if the date from database is near the current date. The problem with my code comes when the month is different. Script works when comparing current day and day from database date, but when the month is different for example 02.05.2012 and 28.04.2012 the script is acting as if the 28.04.2012 is yet to come, he is not taking the month in consideration. I hope the question is clear. Here is the coding so far. Any ideas?

$vrjedi_do_osobna = $row['vrjedi_do_osobna'];

$current_date = date('d.m.Y');

$query = mysql_query("SELECT * from albums where vrjedi_do_osobna = '$current_date'
                                AND $id = 'userid'");




    $ime = $row['dosjename'];
    $prezime = $row['dosje_prezime'];
    $diff = abs(strtotime($current_date) - strtotime($vrjedi_do_osobna));

    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
if($current_date < $vrjedi_do_osobna && $days < 10 && $days > 0 && $years < 1 && $months < 1)
    {
echo"<font face='gregorian' size='2'>";
   printf("&nbsp;<img src='slike_izgled/pj_sat.png' width='20' height='30'/>&nbsp;Osobna  iskaznica radnika $ime $prezime ističe za %d dana\n", $days);
echo"</font><br>";
    }
    if ($vrjedi_do_osobna <= $current_date)
                    {                                           
    echo "                            

&nbsp;<img src='slike_izgled/usklik.gif' width='25' height='25'>&nbsp;<font face='gregorian' size='2'>Osobna iskaznica radnika $ime $prezime-a je istekla!</font>&nbsp;&nbsp;&nbsp;&nbsp;<a href='viewdosje.php?album=".$row['ideo']."'>Pregled dosjea</a><br>

    "; 
// Do something

}

Upvotes: 0

Views: 163

Answers (3)

Anigel
Anigel

Reputation: 3437

Have you looked at the PHP date_diff function http://php.net/manual/en/function.date-diff.php

Alternatively you could Just select the date difference out from mysql as part of your query http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

SELECT *,DATEDIFF(vrjedi_do_osobna,NOW()) from albums where vrjedi_do_osobna = '$current_date' AND $id = 'userid'"

which would return a positive or negative number of days difference

Upvotes: 2

Silviu
Silviu

Reputation: 835

You could store in a variable strtotime($current_date) - strtotime($vrjedi_do_osobna) and use it in the IFs by comparing it to 0 to see if the current date passed or not.

Upvotes: 0

Devator
Devator

Reputation: 3904

Use the strtotime function. You can then compare the digits.

Upvotes: 0

Related Questions