offsetDate
Syntax
string|bool ADOConnection::offsetDate( float $offset optional string $isoDate )
Description
The function offsetDate()
returns a string with the native SQL functions to calculate future and past dates based on $isoDate
in a portable fashion. If $isoDate
is not defined, then the current date (at 12 midnight) is used.
The function returns the SQL string that performs the calculation when passed to Execute()
. The value returned when the statement is executed is an ISO Date.
The function returns false if invalid parameters are provided.
Passing a decimal offset less than one without specifying a base date will always return the current date because the calculation time basis is always the beginning of the current date
Inputs
The $offset is the number of days that should be added or subtracted from the reference date; the decimal portion is the fraction of a day (e.g. 0.5 = 12 hours)
The $isoDate can be:
- A basis date in ISO format including a time portion, if a fractional calculation is required
- An SQL statement that produces an ISO date
- An SQL column if the method is to be used to calculate date offsets based on table values
- skipped or false for today
Examples
Get date one week from now
$fld = $conn->OffsetDate(7); /* * returns "(trunc(sysdate)+7") */
Oracle: get date and time that is 60 hours (2.5 days) from current
$fld = $conn->OffsetDate(60/24, $conn->sysTimeStamp); /* * returns "(sysdate+2.5)" */ $conn->Execute("UPDATE TABLE SET dodate=$fld WHERE ID=$id");
SQL Server: Get Date -273.5 Hours in the past
$fld = $db->offsetDate(-273.5/24); /* * Returns " SELECT FORMAT(DATEADD(hour ,-273.5,GETDATE()), 'yyyy-MM-dd')" */
Generating A Date From a column Value. In this example we would use the statement to return a date 7 days forward from the current value of the column “delivery date”.
$newDeliveryDate = $db->offsetDate(7, 'delivery_date'); $SQL = "SELECT $newDeliveryDate FROM delivery_master";