note 104485 added to function.mssql-bind
| From: | asimonassi at osu1 dot php dot net | Date: | Fri, 17 Jun 2011 14:51:10 +0000 |
| Subject: | note 104485 added to function.mssql-bind | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180643@lists.php.net to get a copy of this message | ||
In order to bind DATETIME, i suggest to convert the date to double, then use SQLFLT8 instead of
SQLVARCHAR.
In SQL Server Datetime is a standard double. The integer part represent the number of days since
1-1-1900 and the fractional part represent the fraction of the day (e.g: 0.5 means noon, 0.75 means
6 PM).
Using SQLVARCHAR may lead to errors depending on the local server config.
<?php
function PhpTimeToOLEDateTime($timestamp)
{
$a_date = getdate ($timestamp);
$year= $a_date['year']; //this year
$partial_days = ($year-1900)*365;//days elapsed since 1-1-1900
//let's calculate how many 29 february from 1900 to first day on this year
$partial_days +=(int)(($year-1) / 4); //each 4 years a leap year since year 0
$partial_days -= (int)(($year-1) / 100); //each 100 years skip a leap
$partial_days += (int)(($year-1) / 400); //each 400 years add a leap
$partial_days -= 460; //459 leap years before 1900 + 1 for math (year 0 does not exist)
$partial_days += $a_date['yday'];
$seconds = $a_date['hours'] * 3600;
$seconds += $a_date['minutes'] * 60;
$seconds += $a_date['seconds'];
$d = (double) $partial_days;
$d += ((double)$seconds)/86400.0;
return $d;
}
?>
Sample binding
<?php
$now = PhpTimeToOLEDateTime(time());
mssql_bind($proc, "@dateparam", $now, SQLFLT8, false);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 213.156.53.66
----
Manual Page -- http://www.php.net/manual/en/function.mssql-bind.php
Edit -- https://master.php.net/note/edit/104485
Del: integrated -- https://master.php.net/note/delete/104485/integrated
Del: useless -- https://master.php.net/note/delete/104485/useless
Del: bad code -- https://master.php.net/note/delete/104485/bad+code
Del: spam -- https://master.php.net/note/delete/104485/spam
Del: non-english -- https://master.php.net/note/delete/104485/non-english
Del: in docs -- https://master.php.net/note/delete/104485/in+docs
Del: other reasons-- https://master.php.net/note/delete/104485
Reject -- https://master.php.net/note/reject/104485
Search -- https://master.php.net/manage/user-notes.php