doudiyu1639 2014-11-05 04:26
浏览 30
已采纳

PHP strtotime返回整年而不是一天

I am currently using PHP ROUND , ABS , STRTOTIME to calculate the difference between two dates.

The calculation works until you select a $_SESSION['b_checkout'] from a new year. i.e. if the b_checkin is Dec 30 and the b_checkout is Dec 31, this returns the correct $no_nights as 1 day.

$_SESSION['b_checkin'] and $_SESSION['b_checkout'] are using 'D F jS Y' date format. e.g.

$_SESSION['b_checkin'] = "Wednesday, 31 December, 2014"
$_SESSION['b_checkout'] = "Thursday, 1 January, 2015"

$no_nights = round(abs(strtotime($_SESSION['b_checkout']) - strtotime($_SESSION['b_checkin']))/(60*60*24));

Currently this outputs (echo $no_nights) 363 days instead of 1 day. What is the problem?

  • 写回答

2条回答 默认 最新

  • duankangpazhuo0347 2014-11-05 04:40
    关注

    Remove all the commas and see it works!

    $b_checkin = "Wednesday 31 December 2014";
    $b_checkout = "Thursday 1 January 2015";
    
    $no_nights = round(abs(strtotime($b_checkout) - strtotime($b_checkin))/(60*60*24));
    echo $no_nights;
    

    When php can't interpret the year correctly, it uses the current year for parsing. thats how you are getting 363 days as result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源