How to use the start and end date of promotion from the CodeIgniter database?

I want to get to start_dateand expire_datefrom promotionfrom the CodeIgniter database, so that the promotion automatically appears on start_dateand disappears after expire_date.
How can I achieve this?

This is the database layout:

promotion:
id(primary,autmatic),
image(varchar,255),
description(longtext),
start_date(datetime),
expire_date(datetime),

An example of the content is as follows:

id image description start_date expired_date
1  1.jpg textpromo1   1/10/2015 5/10/2015  
2  2.jpg textpromo2   8/10/2015 22/10/2015
2  3.jpg textpromo3   1/11/2015 12/11/2015

And this is my view request code

<?php $query = $this->db->get('promotion'); ?>
<?php foreach($query->result() as $val): ?>
<div class="promotion">
<p><?php echo image; ?></p>
<p><?php echo description; ?></p>
<p><?php echo start_date; ?></p>
<p><?php echo expired_date; ?></p>
 </div>
<?php endforeach; ?>

Result:

 1.jpg textpromo1   1/10/2015 5/10/2015  
  2.jpg textpromo2   8/10/2015 22/10/2015
  3.jpg textpromo3   1/11/2015 12/11/2015
+4
source share
2 answers

According to the discussion, you want to receive records between the beginning and the expiration date

SELECT * 
FROM promotion 
WHERE start_date <= '2015-10-23' AND expire_date >= '2015-10-23'

In codeigniter do this,

 <?php 
   $this->db->where('start_date <= ' , date('Y-m-d')); 
   $this->db->where('expired_date >= ' , date('Y-m-d')); 
 ?>

You can take today's date in some variable and use it in the query.

+1

, .

$this->db->where('start_date >= ' , date('Y-m-d'));
$this->db->where('expire_date <= ' , date('Y-m-d'));
$query = $this->db->get('promotion');

https://ellislab.com/codeigniter/user-guide/database/active_record.html

0

Source: https://habr.com/ru/post/1612875/


All Articles