【问题标题】:How do I get the current year plus the next year with php for my wordpress function?如何使用 php 为我的 wordpress 函数获取当前年份和下一年?
【发布时间】:2021-05-20 15:18:26
【问题描述】:

我想创建一个返回的简码函数: 2021 - 2022

然后它会在明年自动更新。

这是我到目前为止的(损坏的)代码:

function current_class_year(){
    return date('Y'); // current year
    return date('Y', strtotime('+1 year')); // next year
}
add_shortcode( 'current_class_year', 'current_class_year' );

感谢您的帮助。

【问题讨论】:

  • 这能回答你的问题吗? How can I combine two strings together in PHP?
  • 您希望合并两个字符串,然后返回。你现在不能工作,因为return 终止了函数,所以第二行永远不会执行。
  • 这是正确的想法,但是当我尝试它时它在函数中不起作用。或者至少不知道怎么做。

标签: php wordpress function shortcode


【解决方案1】:

试试这个

function current_class_year(){
    return date('Y') .' - '.date('Y', strtotime('+1 year'));
}
add_shortcode( 'current_class_year', 'current_class_year' );

【讨论】:

    【解决方案2】:

    函数 current_class_year(){

        return date('Y').' - '.date('Y', strtotime('+1 year')); // Will return : current year - next year
    

    }

    【讨论】:

    • 这也很有魅力!非常感谢!!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多