php check if date is today or greater. but only in

2019-09-14 17:04发布

I have a list of dates and need to enforce a limit, which is the list to not be older than today but more than 30 days ahead of today.

Is possible to modify what I have or do I need to use something like 'time' instead? if so how would I adjust what I have?

$date = new DateTime($page->meta_value);

if (strtotime($page->meta_value) > time()) {
    echo '<h2><div class="date-title">';
    echo $page->post_title.' - ';
    echo '</div><div class="date-date">';
    echo $date->format('d-m-Y').'<br/>';
    echo '</div></h2>';
}

Thanks for reading :)

2条回答
混吃等死
2楼-- · 2019-09-14 17:40

Try this:

if (strtotime($page->meta_value) >= time() && strtotime($page->meta_value) < strtotime('+30 days')) {

Or up to this month only?

if (strtotime($page->meta_value) >= time() && strtotime($page->meta_value) < strtotime('first day of next month')) {
查看更多
叼着烟拽天下
3楼-- · 2019-09-14 17:46

Things that can help you:

print date('Y-m-d',strtotime('last day of this month'));

http://php.net/manual/en/datetime.formats.relative.php

print date('Y-m-d',strtotime('+ 30 days'));

http://php.net/manual/en/datetime.formats.date.php

Read more here http://php.net/manual/en/datetime.formats.php

查看更多
登录 后发表回答