PHP-MSSQL Date problem!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SalokinX
    New Member
    • Jan 2008
    • 4

    PHP-MSSQL Date problem!

    Hi everyone. I am trying to make a table that when people edit something from it, it will show the date it was edited.

    This is my code:

    [PHP]$date = date("m/d/Y");

    mssql_query("IN SERT INTO news2 (title, dtime, text1, text2) VALUES ('$title', $date, '$text1', '$text2')");[/PHP]

    The thing is that when I check the table, the time is messed up. It shows 4/11/1900 instead of 01/20/2008.

    And if its possible, is there a way I could change the format of the date at the MSSQL? I need something like this:12:55 01/20/2008.

    Btw, the data type of the column is datatime.

    Thank you
    ~ SalokinX
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Originally posted by SalokinX
    Hi everyone. I am trying to make a table that when people edit something from it, it will show the date it was edited.

    This is my code:

    [PHP]$date = date("m/d/Y");

    mssql_query("IN SERT INTO news2 (title, dtime, text1, text2) VALUES ('$title', $date, '$text1', '$text2')");[/PHP]

    The thing is that when I check the table, the time is messed up. It shows 4/11/1900 instead of 01/20/2008.

    And if its possible, is there a way I could change the format of the date at the MSSQL? I need something like this:12:55 01/20/2008.

    Btw, the data type of the column is datatime.

    Thank you
    ~ SalokinX
    The reason why it is showing up this way is because you are formating it for the month, day, year with date("m/d/Y"). In your insert statement you could just use 'now()' instead of '$date'.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      how about:
      [php]
      <?php
      echo date("h:m m/d/Y")
      ?>
      [/php]
      Last edited by Markus; Jan 20 '08, 11:08 AM. Reason: Silly americans!

      Comment

      • SalokinX
        New Member
        • Jan 2008
        • 4

        #4
        Originally posted by stepterr
        The reason why it is showing up this way is because you are formating it for the month, day, year with date("m/d/Y"). In your insert statement you could just use 'now()' instead of '$date'.
        I tried the 'now()' and I get this error:

        Warning: mssql_query() [function.mssql-query]: message: Syntax error converting datetime from character string. (severity 16) in C:\xampp\htdocs \news2.php on line 22

        Warning: mssql_query() [function.mssql-query]: Query failed in C:\xampp\htdocs \news2.php on line 22


        I think I know what it means, but I have no idea how to fix it. Any ideas?

        And does anyone know how I could change the date format on MSSQL? I wanted it to be like this: 12:45 30/12/2008

        Thank you
        ~ SalokinX

        Comment

        • SalokinX
          New Member
          • Jan 2008
          • 4

          #5
          Anyone?

          Please, this is urgent.

          Thank you!
          ~ SalokinX

          Comment

          • SalokinX
            New Member
            • Jan 2008
            • 4

            #6
            I fixed the problem... I used a pretty longer code, but it worked.
            [PHP] $dia = gmdate(d);
            $mes = gmdate(m);
            $ano = gmdate(Y);
            $minuto = gmdate(i);
            $date = gmdate(H);
            $brdate = $date - 2;
            $brdate2 = $brdate;

            if($brdate < 0)
            {
            $brdate2 = $brdate + 12;
            $ampm = "PM";
            }
            elseif($brdate == 0)
            {
            $brdate2 = $brdate + 12;
            $ampm = "AM";
            }
            elseif($brdate <= 11)
            {
            $ampm = "AM";
            }
            elseif($brdate == 12)
            {
            $brdate2 == $brdate;
            $ampm = "PM";
            }
            elseif($brdate >= 13)
            {
            $brdate2 = $brdate - 12;
            $ampm = "PM";
            }

            if($brdate2 < 10)
            {
            $hora = "0$brdate2:$min uto$ampm";
            }
            else
            {
            $hora = "$brdate2:$minu to$ampm";
            }

            $adddate = "$hora $dia/$mes/$ano";[/PHP]

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              You were getting the NOW error because you were probably enclosing the function in quotes
              Code:
              SET DATE = 'NOW()' is wrong, should be
              SET DATE = NOW()

              Comment

              Working...