doubiaode0460 2014-04-12 18:24
浏览 27
已采纳

PHP OOP DateTime ModifyDate小数位错误

If I use the following code

$averageWeeks = 2.7;
$laidDate->modify('+'.$averageWeeks.' week');

I get the date 31st May 2014 returned, whereas if i use the following code

$averageWeeks = 3;
$laidDate->modify('+'.$averageWeeks.' week');

I get the date 3rd May 2014 which is right. Is there any way I can use decimal places in the date modify method?

  • 写回答

1条回答 默认 最新

  • duanlu1793 2014-04-12 18:48
    关注

    Assuming you're starting at April 12th, then May 3rd would be 3 weeks and May 31st would be 7 weeks. So my guess is that when you pass "2.7", it is ignoring the "2." portion of that string.

    You could consider calculating the number of days to add separately, such as:

    $averageWeeks = 2.7;
    $averageDays = floor($averageWeeks * 7);
    $laidDate->modify('+'.$averageDays.' days');
    

    Consider also that 2.7 weeks is 18.9 days. By taking the floor, We are adding 18 days. It doesn't make since to add fractional days here, unless you intend to change the time of day as well. If you did, you'd have to do some additional math, as it doesn't appear that either modify or add will support fractional values. You could do something like this:

    $averageWeeks = 2.7;
    $averageSeconds = floor($averageWeeks * 7 * 24 * 60 * 60);
    $laidDate->modify('+'.$averageSeconds.' seconds');
    

    But you might be fooling yourself if you think this time is accurate, since not every local day has exactly 24 hours in it, due to daylight saving time transitions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100