doumang20060820 2017-08-12 21:13
浏览 28
已采纳

如何在日期差异功能中使用变量?

I am using an ACF Field to allow a client to content manage a countdown to their next event I am using JS flip clock on the desktop version but as it isn't responsive, I decided to use date diff to echo out just the number of days for mobile.

The site is currently live at theindustrialproject.co.uk

The code I currently have is this:

<?php
    $date1 = date_create(date());
    $date2 = date_create(the_field('mobile_date'));
    $diff = date_diff($date1,$date2);

    $difference = $diff;

    if ($difference < 0) { $difference = 0; }
    echo '<span class="countdown-mobile">'. floor($difference/60/60/24)."</span><br />";
    if ($difference == 1) { echo "<p>Day</p>"; }
        else { echo "<p>Days</p>"; }
?>

but it always returns 0. For reference, I pulled the code from here

  • 写回答

1条回答 默认 最新

  • dongshang1768 2017-08-12 21:55
    关注

    Without knowing what the function the_field('mobile_date') will return ( either a date or timestamp? ) you might need to alter that particular line below but you should be able to use the DateTime object and format the difference like this

    $format='Y-m-d';
    $timezone=new DateTimeZone('Europe/London');
    
    /* We need 2 datetime objects - one for now the other for the future date */
    $now=new DateTime( date( $format, strtotime('now') ), $timezone );
    $target=new DateTime( the_field('mobile_date'), $timezone );
    
    /* Format the difference in days */
    $days = $now->diff( $target )->days;
    
    echo "<span class='countdown-mobile'>{$days}</span><br />" . ( $days == 1 ? '<p>Day</p>' : '<p>Days</p>' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?