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 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法