网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

比较wordpress中的日期

GG网络技术分享 2025-03-18 16:12 3


问题描述:

I am using following code in WordPress to check if articles are posted before 15-09-17 but the problem is if the date is greater than 15 and month is less it still shows the day is greater. For example if $pdate is 28-08-17 and $mydate is 15-09-17 it still return as true.

    $pdate  = strtotime(get_the_date(\"d-m-y\"));

$mydate = strtotime(\'15-09-17\');

if ($pdate>$mydate)

{

//do something

}

else {

//do something

}

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我在WordPress中使用以下代码来检查文章是否在15-09-17之前发布但问题是如果 日期大于15,月份少于它仍显示日期更大。 例如,如果$ pdate是28-08-17而$ mydate是15-09-17,它仍然返回true。</ p>

  $ pdate = strtotime(get_the_date(“dmy”)  )); 

$ mydate = strtotime(\'15 -09-17\');

if($ pdate&gt; $ mydate)

{

//做某事

}

其他{

// do 某事

}

</ code> </ pre>

</ div>

网友观点:

It\'s interpreting your first numbers (i.e. 28 and 15) as the year.

From the strtotime() manual:

If, however, the year is given in a two digit format and the separator

is a dash (-, the date string is parsed as y-m-d.

Try using a four digit year so it doesn\'t parse the date string in an undesirable way:

$pdate  = strtotime(get_the_date(\\\"d-m-Y\\\"));

$mydate = strtotime(\'15-09-2017\');

if ($pdate>$mydate)

{

//do something

}

else {

//do something

}

###

You\'ve specified the format of $pdate using d-m-y; however, $mydate follows the format set in Settings > General. Make sure the two formats match to have correct comparison.

To clarify, let the code reflect the date format in your setting. Say you want the date in your posts to appear like so: September 20, 2017, which is F j, Y. Your code will be:

$pdate  = strtotime(get_the_date(\'F j, Y\'));

$mydate = strtotime(\'September 15, 2017\');

if ($pdate>$mydate)

{

//do something

}

else {

//do something

}

标签:

提交需求或反馈

Demand feedback