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

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误