dougu6815 2016-07-15 02:42
浏览 60
已采纳

PHP strtotime仅在添加天数时正确转换

I am using strtotime in PHP, and getting two different result from the same input. I cannot work out why.

$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));
$newDate = date('d/m/Y', strtotime('+3 weekdays', $startDate));

The original date is 14/07/2016 01.33 PM

$newDate is returning 19/07/2016 As expected.

$oldDate is returning 01/01/1970 Not the expected result - should be 14/07/2016.

I have tried other functions inside strtotime and they all produce the correct result. What am I missing? Why can I not simply just pass 1468467219 to strtotime without modifying it?

  • 写回答

2条回答 默认 最新

  • dongwei4096 2016-07-15 03:37
    关注

    You are misusing strtotime. This function takes a string representation of a date, and returns a timestamp. Instead you're feeding it a timestamp

    $startDate = 1468467219;
    $oldDate = date('d/m/Y', strtotime($startDate));
    

    Since no common date format is expressed as "today is 1468467219", the function is unable to parse it and returns false.

    var_dump(strtotime($startDate)) //<-- boolean FALSE
    

    When you go on to feed FALSE to the date function, it can't parse it either, so it returns the wrong date: 01/01/1970.

    To get the result you need just feed the timestamp directly to date:

    $oldDate = date('d/m/Y', $startDate);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分