[英]ACF DateTime/Date Picker : get posts greater than the current date
這里的情況:
日期字段
$start_date = get_sub_field('start_date');
$date = DateTime::createFromFormat('F j, Y', $start_date);
$sdate = $date->format('F j, Y');
wordpress 中的當前日期
$current_date = date('F j, Y');
我想設置一個條件,即它只顯示大於當前日期的字段。
所以當我這樣做的時候
if ($sdate > $current_date) { }
結果並不如預期,因為它也顯示了較早月份的值。 我做錯了什么?有幫助嗎?
使用以下代碼。 它可以幫助你
如果 (strtotime($sdate) > strtotime($current_date)) { }
您似乎在與字符串進行比較,而不是 dateTime 對象。 因此 php 將根據字符串開頭的字符對它們進行排序。
2019 年 12 月 3 日將被視為小於 2019 年 1 月 5 日,因為 D 在字母表中排在第一位。
你可以通過像這樣格式化它們來解決這個問題(如果你想比較字符串而不是 dateTime 對象本身):'Ymd'
您現在正在比較數字,從年份開始,然后是帶有前導零的月份,然后是帶有前導零的日期。
startTimes = 'Your date';
$args = array(
'posts_per_page' => -1,
'post_type' => 'post_type',//Your post type
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $startTimes, //Please confirm date format with your database
'compare' => '>='
)
)
);
$pre_date_posts = get_posts($args);
_e('<pre>');
print_r($pre_date_posts); //Your Result
_e('</pre>');
<script>
$(function () {
$('#id_name').datepicker({
format: 'dd/mm/yyyy',
autoclose: true,
startDate: new Date()
});
});
</script>
請按照以下步驟操作:
請安裝ACF以從后端生成動態字段。
<div class="container"> <?php $sDate = get_field('start_date'); $currendDate = date('F j, Y'); if($sDate > $currendDate){ echo 'Yes, It is greater.'; }else{ echo 'No, It is not greater.'; } ?> </div>
見附件截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.