Reputation: 80
Initially when i was using php_msssql extension the following line was working
$test2_date = date('d-M-y', strtotime( "".$res["test_date"]." +".$noofdays." days" ));
$res
is array.
Then i changed to php_sqlsrv_53_ts_vc9
extension due to which the above line was not working because "date" doesn't work in this extension so i used "date_format" which works in sqlsrv. My code line was like shown below
$test2_date = date_format(strtotime( "".$res["test_date"]." +".$noofdays." days" ), 'd-M-y');
but the strtotime is not working and hence i get the below error:-
Catchable fatal error: Object of class DateTime could not be converted to string.
what should i use to add days to the date?
Upvotes: 0
Views: 190
Reputation: 29943
If you want to retrieve date and time types (datetime, date, time, datetime2, and datetimeoffset) as strings, you have following options:
Set connection option ReturnDatesAsStrings
to false
and format returned date field value. This is by default.
$server = 'server\instance,port';
$cinfo = array(
"ReturnDatesAsStrings"=>false,
"Database"=>'database',
"UID"=>"user",
"PWD"=>"password"
);
...
$test2_date = date_format(date_add($res["test_date"], date_interval_create_from_date_string($noofdays." days")), 'd-M-y')
...
Set connection option ReturnDatesAsStrings
to true
.
$server = 'server\instance,port';
$cinfo = array(
"ReturnDatesAsStrings"=>true,
"Database"=>'database',
"UID"=>"user",
"PWD"=>"password"
);
...
$test2_date = date_format(date_add(date_create($res["test_date"]), date_interval_create_from_date_string($noofdays." days")), 'd-M-y')
...
Additional information can be found here.
Upvotes: 0
Reputation: 4840
date_format take date object as first parameter. So need to create date object with date_create first. Code should be:
$date = date_create($res["test_date"]);
date_add($date, date_interval_create_from_date_string(" +" . $noofdays . " days"));
echo date_format($date, "d-M-y");
UPDATE:
If you have date object already then date_create
step can be skipped and code will be like:
date_add($res["test_date"], date_interval_create_from_date_string(" +" . $noofdays . " days"));
echo date_format($date, "d-M-y");
Upvotes: 1