PHP-MSSQL Date Problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SalokinX
Forum Newbie
Posts: 3
Joined: Mon Jan 21, 2008 10:46 am

PHP-MSSQL Date Problem!

Post 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:

Code: ( php )

Code: Select all

$date = date("m/d/Y");
mssql_query("INSERT INTO news2 (title, dtime, text1, text2) VALUES ('$title', '$date', '$text1', '$text2')");
The thing is that when I check the table, the time is messed up. It shows 4/11/1900 instead of 01/21/2008.

I tried using now() instead of '$date' but I get the following 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


Also, is there a way I can add the time next to the date into the database?

Btw, is there a way I could insert the date into the database like this: 12:50am 31/12/2008

Thank you
~ SalokinX
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP-MSSQL Date Problem!

Post by Christopher »

You will need to convert date you YYY-MM-DD format to insert them. It is a pretty easy set of substring checks, or you could use the date functions.
(#10850)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP-MSSQL Date Problem!

Post by califdon »

Learn about MySQL date-time functions: http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
SalokinX
Forum Newbie
Posts: 3
Joined: Mon Jan 21, 2008 10:46 am

Re: PHP-MSSQL Date Problem!

Post by SalokinX »

Im not using MySQL but MSSQL, not sure if it works the same for both.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP-MSSQL Date Problem!

Post by califdon »

SalokinX wrote:Im not using MySQL but MSSQL, not sure if it works the same for both.
Oops, sorry. I didn't read closely enough. But check MSSQL documentation for the same thing.
SalokinX
Forum Newbie
Posts: 3
Joined: Mon Jan 21, 2008 10:46 am

Re: PHP-MSSQL Date Problem!

Post by SalokinX »

Got it to work using this code:

Code: Select all

    $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:$minuto$ampm";
    }
else
    {   
    $hora = "$brdate2:$minuto$ampm";
    }
 
    $adddate = "$hora $dia/$mes/$ano";
Post Reply