จะใช้ EntityFieldQuery กับวันที่ได้อย่างไร [ปิด]


9

ฉันต้องเลือกรายการที่มีวันที่ระบุ

ควรจัดรูปแบบต่อไปนี้อย่างไร ฉันต้องแยกวิเคราะห์วันที่ 15 พฤษภาคม 2010 หรือไม่

นอกจากนี้ฉันจะได้รับหน้าข้อผิดพลาด

function events2() {

$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('status', 1, '=')
  ->propertyCondition('type', 'event')  
  ->propertyCondition('field_event_date', '15-May-2010', '=');

$result = $query->execute();

 return $result;

}

1
ไม่สามารถตอบได้เนื่องจากเป็นเพราะเราไม่ทราบประเภทฟิลด์สำหรับ field_event_date

คำตอบ:


11

ด้วย Drupal 7 และโมดูล Date 2.2:

$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('status', 1) // Getting published nodes only.
->fieldCondition('field_dates', 'value2', date("Y-m-d"), '<') // end date before today
->execute();

สิ่งนี้ใช้ได้สำหรับฉันเมื่อฉันเปลี่ยน 'value2' เป็น 'value'
Craig

1
โปรดทราบว่า 'ค่า' สำหรับวันที่เริ่มต้นและ 'ค่า 2' สำหรับวันที่สิ้นสุดหากกำหนดค่า
Mario Awad

2

ฉันไม่ได้ใช้EntityFieldQueryแต่ดูรหัสแนะนำว่าคุณจะต้องตรวจสอบให้แน่ใจว่าfield_event_dateเก็บไว้เป็นเขตข้อมูลMySQL DateTimeและรูปแบบของการโต้แย้งเป็นหนึ่งในรูปแบบที่ต้องการ:

เป็นสตริงในรูปแบบ 'YYYY-MM-DD' หรือ 'YY-MM-DD' อนุญาตให้ใช้ไวยากรณ์ "ผ่อนคลาย" ที่นี่เช่นกัน ตัวอย่างเช่น '98 -12-31 ', '98 .12.31', '98 / 12/31 'และ '98 @ 12 @ 31' จะเทียบเท่ากัน


0

นี่คือรหัสสำหรับการตรวจสอบช่วงวันที่โดยใช้คำหลักระหว่าง

$month = $form_state['values']['month'];
$year = $form_state['values']['year'];
$num_padded = sprintf("%02d", $month);
$first_day = date($year.'-'.$num_padded.'-01 00:00:00'); 
$last_day =  date("Y-m-t 23:59:59", strtotime($first_day));
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
            ->entityCondition('bundle', 'YOUR_CONTENT_TYPE')
            ->fieldCondition('DATE_FIELD', 'value', array($first_day,$last_day), 'BETWEEN');
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.