【问题标题】:How to add [today's date +1 day]如何添加[今天的日期+1天]
【发布时间】:2022-01-31 21:20:13
【问题描述】:

我目前在 wordpress 中使用此代码:

// [date]  
function displaydate(){  
return date('l, F jS, Y');  
}  
add_shortcode('date', 'displaydate');  
// end date

--

我想做的是添加另一个显示今天日期+1天的短代码。

所以我可以这样写:

'...优惠用完 [今天日期 +1 天]'

有人知道怎么做吗?

【问题讨论】:

  • 使用 javascript 或 jquery 或..... ????
  • 抱歉,我不确定,无论是 wordpress 编辑器中的主题 functions.php 中的内容。

标签: php wordpress date future


【解决方案1】:

PHP 中最简单的方法是使用strtotime 函数。

至于您要添加的代码:

return date('l, F jS, Y', strtotime('+1 day'));

根据您的原始代码,名为“明天”的“简码”如下所示:

// [tomorrow]  
function displaydate_tomorrow(){  
    return date('l, F jS, Y', strtotime('+1 day')); 
}  
add_shortcode('tomorrow', 'displaydate_tomorrow'); 
// end tomorrows date

【讨论】:

  • 嘿,你介意给我完整的代码,以便我可以将它复制并粘贴到 wordpress 函数编辑器中吗?我玩过代码,但我不知道如何添加短代码,以便稍后在编辑我的 wordpress 页面/帖子时“调用”它。
  • 您将在您发布的现有代码下方添加上述内容。新的短代码将被称为“明天”,正如 add_shortcode 函数中的第一个参数所指定的那样。
【解决方案2】:

PHP 文档在日期操作方面非常全面,因此找到合适的解决方案应该很容易。这是一种可能的解决方案:

function displaydate($plus_days) {
  return date('l, F jS, Y', strtotime('+' . $plus_days . ' day'));
}

http://uk3.php.net/manual/en/function.strtotime.php

【讨论】:

  • 谢谢。我在其中添加了它,但它说我不能这样做,因为我之前已经声明了“显示日期”。我该如何解决这个问题?我要为它添加短代码吗?
【解决方案3】:

这是我能找到的对这个问题的唯一直接答案,非常感谢您提供答案。

我还使用了变量,因为我想显示一个准确的日期,并且当我在多伦多地区时,通过将日期更改为小时并输入 -5 到 GMT 时间,效果非常好!

对于新手,我使用的完整代码如下:

 function displaydate(){
     return date('l, F jS, Y', strtotime('-5 hours')); 

 }
 add_shortcode('date', 'displaydate');

然后,我在希望日期出现在我的 wordpress 页面上的页面上输入以下内容:

Last update of this page: [date]

结果很完美。

【讨论】:

    【解决方案4】:

    我用这个 sn-p 来计算简单的预计交货日期。它基于 dianovich sn-p 但他(她)的 sn-p 返回错误。这工作正常:

    function displaydate( $atts ) {
        $atts = shortcode_atts( array(
            'day'   => '0'     
        ), $atts );
        $plus_days = $atts['day'];
        return date_i18n('j F Y', strtotime('+' . $plus_days . ' day'));
    }
    add_shortcode('delivery_date', 'displaydate');
    

    显示的简码: [delivery_date day="5"]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2021-06-16
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多