0

I'm trying to finish my project and currently stuck with mysql date field. What I'm trying to do is: if there is no date set then show "ei ole määritelty" text. However, the code is throwing the default date which is 01.01.1970.

I'm checking if the field is not null and when I use if condition I'm saying if($aloitus_pvm3_tulos_result > 0) which doesn't seem right.

this is my part of the code and I hope that you could show me my mistake.

if(mysql_num_rows($tilaukset1)>0)
{
    echo "<h3>Muut Tilaukset</h3>";

    $tilaukset = mysql_query("SELECT * FROM tilaus WHERE user_id = '".$id."' AND status != 'TILATTU'");


    echo "<table border='1'>";
    echo "<tr>";
    echo "<th> Tilaaja </th>";
    echo "<th> Työn kuvaus </th>";
    echo "<th> Tilaus pvm </th>";
    echo "<th> status </th>";
    echo "<th> aloituspvm</th>";
    echo "<th> valmistumispvm </th>";
    echo "<th> hyvaksymispvm </th>";
    echo "<th> tehtytyo </th>";
    echo "<th> tunti </th>";
    echo "<th> tarvikkeet </th>";
    echo "<th> kustannukset </th>";
    echo "</tr>";
    while($row = mysql_fetch_array($tilaukset))
    {



        $tilattu_pvm3_tulos = mysql_query("SELECT * FROM tilaus WHERE tilauspvm IS NOT NULL");
        $tilattu_pvm3_tulos_result = mysql_fetch_array($tilattu_pvm3_tulos);

        $aloitus_pvm3_tulos = mysql_query("SELECT * FROM tilaus WHERE aloituspvm IS NOT NULL");
        $aloitus_pvm3_tulos_result = mysql_fetch_array($aloitus_pvm3_tulos);

        $valmis_pvm3_tulos = mysql_query("SELECT * FROM tilaus WHERE valmistumispvm IS NOT NULL");
        $valmis_pvm3_tulos_result = mysql_fetch_array($valmis_pvm3_tulos);

        $hyvaksymis_pvm3_tulos = mysql_query("SELECT * FROM tilaus WHERE hyvaksymispvm IS NOT NULL");
        $hyvaksymis_pvm3_tulos_result = mysql_fetch_array($hyvaksymis_pvm3_tulos);

        if($tilattu_pvm3_tulos_result > 0)
        {
            $tilattu_pvm3 = date ('d.m.Y', strtotime($row['tilauspvm']));
        }
        else
        {
            $tilattu_pvm3 =  'ei ole määritelty';
        }

        ///////////////////////////////////////////////////////////////////////////////

        if($aloitus_pvm3_tulos_result > 0)
        {
            $aloitus_pvm3 = date('d.m.Y', strtotime($row['aloituspvm']));
        }
        else
        {
            $aloitus_pvm3 =  'ei ole määritelty';
        }

        ///////////////////////////////////////////////////////////////////////////////

        if($valmis_pvm3_tulos_result > 0)
        {
            $valmis_pvm3 = date('d.m.Y', strtotime($row['valmistumispvm']));
        }
        else
        {
            $valmis_pvm3 =  'ei ole määritelty';
        }

        ////////////////////////////////////////////////////////////////////////////////


        if($hyvaksymis_pvm3_tulos_result > 0)
        {
            $hyvaksymis_pvm3 = date('d.m.Y', strtotime($row['hyvaksymispvm']));
        }
        else
        {
            $hyvaksymis_pvm3 =  'ei ole määritelty';
        }





        echo "<tr>";
        echo "<td>" . $row['tilaaja'] . " </td> 
              <td>" . $row['kuvaus'] . "</td> 
              <td>" . $tilattu_pvm3 . "</td>
              <td>" . $row['status'] . "</td>
              <td>" . $aloitus_pvm3 . "</td>
              <td>" . $valmis_pvm3 . "</td>
              <td>" . $hyvaksymis_pvm3 . "</td>
              <td>" . $row['tehtytyo'] . "</td>
              <td>" . $row['tunti'] . "</td>
              <td>" . $row['tarvikkeet'] . "</td>
              <td>" . $row['kustannukset'] . "</td>";
        echo "</tr>";

    }

    echo "</table>";

}

1 Answer 1

2

Although it will somewhat depend on what you are pulling back if a date is "null" as you say, but the quickest way is in fact to check it it is null:

if(!is_null($tilattu_pvm3_tulos_result['tilauspvm']))

Of course, PHP being what it is, there is a much quicker way:

if($tilattu_pvm3_tulos_result['tilauspvm'])

If it is null, it will return false.

I am a little lost as well with what field you are trying to validate. In your strtotime, you use the result from the outermost while (and pull the data from $row) but when you create an if statement, you use $tilattu_pvm3_tulos_result? Not saying it is wrong, just saying that I don't follow what you are checking - so you might need to adjust my code to best suit your logic.

Sign up to request clarification or add additional context in comments.

2 Comments

I am facing this problem right now. When I try to validate date if it is null or not, let's take register field for example. If one of the register fields for certain user is not null then all the other null register fields for other users will be set to 01.01.1970. I think this is happening because I'm calling it inside row and don't know how to get over this problem
Ok, now I've got rid of the rubbish code ... if else if else..etc. Now my code in while loop looks like this to check if date is null or not and if not change the time format to the desired one and print on sight <td>"; if($row['aloituspvm'] === NULL){ echo "ei ole määritelty";} else { echo date ('d.m.Y', strtotime($row['aloituspvm']));} echo "</td>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.