douju2474 2018-06-29 05:26
浏览 198
已采纳

将日期添加到通过PHP中的sqlsrv_fetch_array返回的日期

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?

  • 写回答

2条回答 默认 最新

  • dsa4214 2018-06-29 05:33
    关注

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。