How can I query for +/- 10 days TheDate in DB query - mysql

Okei,, so I am trying to retrieve some info from my database with a +/- 10 days setting.
Values:
Event.start_date = y-m-d
Event.end_date = y-m-d
My.date = y-m-d
$conditions["My.date >="] = date( 'Y-m-d');
$conditions["My.date -10 >="] = Event.start_date;
$conditions["My.date +10 >="] = Event.end_date;
Any suggestions how I can achive this?

Try this in your query
If you are checking against today's date:
WHERE startDate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 10 DAY)
If you are checking against your own end date:
WHERE startDate BETWEEN endDate() AND DATE_ADD(endDate(), INTERVAL 10 DAY)
For going back - 10 days
WHERE startDate BETWEEN DATE_ADD(startDate(), INTERVAL -10 DAY) AND endDate()
Please adjust logic according to your needs.

This is how :)
$conditions = array(
"OR" => array(
array('My.date BETWEEN DATE_ADD(?, INTERVAL -10 DAY) AND ?' => array($data['Event']['start_date'],$data['Event']['end_date'])),
array('My.date BETWEEN ? AND DATE_ADD(?, INTERVAL 10 DAY)' => array($data['Event']['start_date'],$data['Event']['end_date']))
)
);

Related

Database command to delete users according to time definitions

Users for whom the registration date (timestamp) is older than today-1day and fulfills the condition should be deleted.
Example:
User 1 to 3 have RegDate: 11/21/2022 - HH:MM
User 4 have RegDate: 11/18/2022 - HH:MM
a) RegDate = 11/18/2022 (is stored in the database as a timestamp)
b) Today is 11/22/2022 - 1 day = 11/21/2022 = HH:MM
Since the registration date is before 11/21/2002 and the condition of user 4 is met, he should be deleted.
My attempt:
$db->query("DELETE FROM ".TABLE_PREFIX."users WHERE (regdate < (UNIX_TIMESTAMP() - '86400')) AND hideemail = '1'");
oder
$db->query("DELETE FROM ".TABLE_PREFIX."users WHERE (datediff(now(), `regdate`) >= 1) AND hideemail = '1'");
Regardless of whether I choose < or >, users 1-3 are always deleted but user 4 is not.
The aim should be that only user 4 is deleted and users 1 to 3 are not.
I'm defining something wrong.
Can anyone tell me what I am doing wrong?
Thanks in advance.
Do you want to delete users registered before 2022-11-21 (CURRENT_DATE - INTERVAL 1 DAY) or before 2022-11-21 13:10:17 (NOW() - INTERVAL 1 DAY)
$db->query("DELETE FROM " . TABLE_PREFIX . "users WHERE (regdate < CURRENT_DATE - INTERVAL 1 DAY) AND hideemail = 1");
or
$db->query("DELETE FROM " . TABLE_PREFIX . "users WHERE (regdate < NOW() - INTERVAL 1 DAY) AND hideemail = 1");

Showing data from database 15 mnute earlier and stop showing it 15 minute late from the time set for it

$query = "SELECT title,time_strt,ampmstrt,time_end,ampmend
FROM addfunction
WHERE (NOW() + INTERVAL 15 MINUTE) BETWEEN time_strt AND time_end AND location = 'MAWAR ROOM'";
topo,
i hope i understand it correctly...
select title,time_strt,ampmstrt,time_end,ampmend
FROM addfunction
where UNIX_TIMESTAMP(time_strt) >= (UNIX_TIMESTAMP(NOW()) - 15 * 60)
and UNIX_TIMESTAMP(time_end) >= (UNIX_TIMESTAMP(NOW()) + 15 * 60)
AND location = 'MAWAR ROOM';
This will found all rows start 15 minutes before time_strt and ends 15 minutes after time_end.
Ended doing it like this which show the data from database 15 minute earlier than time_strt and stop Showing after 15 minute have elapsed from the time_end. I changed the data type from DATETIME to TIME. Thanks to evryone and mostly #harley81!
WHERE date = CURDATE() AND time_strt <= (CURTIME() + INTERVAL 15 MINUTE) AND time_end >= (CURTIME() - INTERVAL 15 MINUTE) AND location = 'MAWAR ROOM'";

How can I update TIMESTAMP with Codeigniter and MySQL?

How can I update TIMESTAMP with Codeigniter and MySQL? I want to update with INTERVAL 1 MINUTE
I've tried the next code:
$data = array('is_active' => $state,
'timestamp_demo' => "DATE_ADD(NOW(), INTERVAL 1 MINUTE)");
$this->db->where('demo_session_id', 'web');
$this->db->update('demo_session', $data);
But not work. How can I do?
Try your query like this
$this->db->query("UPDATE demo_session
SET
is_active = 1,
timestamp_demo = DATE_ADD(NOW(), INTERVAL 1 MINUTE)
WHERE demo_session_id = 'web'");
You can do this with Active Records, which you're using:
$this->db->where('demo_session_id', 'web');
$this->db->set('is_active', $state);
$this->db->set('timestamp_demo', 'DATE_ADD(NOW(), INTERVAL 1 MINUTE)', FALSE);
$this->db->update('demo_session');
The third parameter in $this->db->set() prevents CodeIgniter from escaping the data so it'll use the MySQL function.
It'll end up something like this:
UPDATE 'demo_session' SET 'is_active' = $state, 'timestamp_demo' = DATE_ADD(NOW(), INTERVAL 1 MINUTE) WHERE 'demo_session_id' = 'web'

Set early bird registration by date

I have an event date, m_date, 2011-11-11
I have days prior to event, e_date, which is an int, 30
And Now()
How do i perform a query return true if todays date, Now() is < e_date - m_date?
You'll want to use strtotime.
$event_date_in_seconds = strtotime($event_date);
$days_prior = 30;
if (time() < (strtotime("-".$days_prior." second", $event_date_in_seconds)))
return true;
In MySQL, not really sure how you want it to return true, but it'd be something like this:
SELECT * FROM table WHERE NOW() < DATE_SUB(event_date, INTERVAL 30 DAY)

why not pulling data for last week

Why wouldn't this be working for pulling data from last week?
if($_GET['reminder'] == 'lastweek') {
$lastweek = date('Y-m-d', strtotime('last sunday'));
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' AND '$lastweek' - INTERVAL 6 DAY ORDER BY contacttodo.reminder ASC";
This is the definition of BETWEEN:
expr BETWEEN min AND max
so you should put the smaller value (minimum) first:
BETWEEN '$lastweek' - INTERVAL 6 DAY AND '$lastweek'
'$lastweek' - INTERVAL 6 DAY ORDER is smaller than '$lastweek'