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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?