dongsi7759 2012-02-10 19:34 采纳率: 100%
浏览 62
已采纳

在php / Wordpress中留给特定日期的天数

I'm trying to get a simple "XX days left" for a custom post time I'm working with (jobs). I've tried to find the answer everywhere but nothing adapts well, changed my code 1000 times following examples on internet but I couldn't solve.

My code looks like this :

<?php
$today = date('d/m/Y');
$today = strtotime($today);
$finish = get_field('todate');
$finish = strtotime($finish);
//diference
$diff = $finish - $today;
$daysleft=floor($diff/(60*60*24));
echo "$daysleft days left";
?>

on $finish the get_field ('todate') returns the date dd/mm/yyyy

Anyone who can help me please?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongtang9855 2012-02-10 19:46
    关注

    The issue is the arrangement of the d/m/Y (it should be m/d/Y) in your $today and $finish variables (for a listing of valid date formats that work with strtotime, visit the PHP Date Format page):

    <?php    
        $today = date('m/d/Y');
        $today = strtotime($today);
        $finish = get_field('todate');
        $finish = strtotime($finish);
        //difference
        $diff = $finish - $today;
    
        $daysleft=floor($diff/(60*60*24));
        echo "$daysleft days left";
    ?>
    

    Tested and works, assuming the $finish date is entered as m/d/yyyy (not as d/m/yyyy)

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度