doue1925 2014-10-10 10:21
浏览 35
已采纳

使用日期对象计算日期时间的最准确方法

i am working on an access site, paying members will get exactly 3 months access period to the site. the issue therefore is how to calculate an exact 3 month date period.

i.e, some months are 28 days, others are 31 days; normal years is 365 but lunar year is 354 days.

i was thinking of converting the date to a UNIX timestamps and then calculating 3 months in seconds. But i am not sure whether this is the most efficient and accurate way to do it.

below is my proposal , i would really appreciate some advice on it;

timestamp when the clocks starts

$UNIXtimeStampNow   =   new \DateTime("now"))->format('U')

calculating 3 months from date:

  $numberDaysInMonth  = 30.41 = 365/ 12   //number of days in months

  $numberSecondsInDay  = 86400; //number seconds in a day

 $secondsIn3Months  = ($numberDaysInMonth * $numberSecondsInDay) * 3  //number seconds in 3 months

    new \DateTime("$secondsIn3Months"); //convert back to date object

like i said, this is the best i came up with, but i suspect that its not accurate.

would really appropriate some advice

  • 写回答

3条回答 默认 最新

  • dongzongxun8491 2014-10-10 10:42
    关注

    As I said in my comment, just use the DateTime object's add() method with a DateInterval

    $d = new \DateTime("now");
    $d->add(new \DateInterval('P3M'));
    echo $d->format('Y-m-d H:i:s');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧