douan7601 2014-05-25 08:12
浏览 190
已采纳

PHP在一周后获取日期并计算剩余天数

I have a dynamic date, now what i want is that finding the date after exact one week, i have achieved that with the code below, but now i want that now many days are left for that week after date to come. i have got some sort of time stamp, but i don't know how to convert it to DAYS LEFT.

$weekDate = date( "d/m/Y", strtotime("19-05-2014") + 86400 * 7 );

echo $weekDate;// THATS PERFECT

////////////////////////////////////////////////////////////////

$future = strtotime( $weekDate ); //Future date.

$datediff = time() - $future;

$days = floor( ( ( $datediff / 24 ) / 60 ) / 60  ); //this is not perfect, returns some 

sort of timestamp

I have tried other methods which are fine, but if week completes on 26, and today is 25th it gives me 0 days left, but it should say 1 day left. please help me.

  • 写回答

3条回答 默认 最新

  • doulu7921 2014-05-25 09:32
    关注

    In your $date_diff now is less than the future date thats why its zero. Inside strtotime() function, you can directly put a relative date inside. In this case, for one week you can use +1 week or +7 days. Consider this example:

    $next_week = date('d/m/Y', strtotime('19-05-2014 +1 week')); // 26/05/2014
    $next_week = strtotime('19-05-2014 +7 days');
    $difference = $next_week - time(); // next weeks date minus todays date
    $difference = date('j', $difference);
    echo $difference . (($difference > 1) ? ' days ' : ' day ') . ' left';
    // should output: 1 day left
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀