Between Condition is not working on server side - php

Between Condition is not working on server side
$this->db->group_start();
$this->db->where('("'.$start_date.'" BETWEEN start_date AND end_date)', NULL, FALSE);
$this->db->or_where('("'.$end_date.'" BETWEEN start_date AND end_date)', NULL, FALSE);
$this->db->group_end();
This Between Condition is working on local but not on server side

maybe you database from server vs local doesn't have the same data, or maybe different format of dates to validate.

Put this Condition
('SELECT id
FROM bookings
WHERE id = "'.$caretaker_id.'"
AND booking_status = "Active"
AND ((booking_start_date <= "'.$start_date.'"
AND booking_end_date >= "'.$start_date.'")
OR (booking_start_date <= "'.$end_date.'"
AND booking_end_date >= "'.$end_date.'"));');

Related

Compare date() to timestamp PHP

Is there a way that I can compare a day with date()on php and timestamp() on db like this
example:
date(); has this value 2021-11-02
timestamp() has this value 2021-11-02 11:00:52
here's my code
$current_date = date("Y-m-d");
$fetch_user_by_number = "SELECT id,transfer_amount,number FROM `transfer_wallet` WHERE `transfer_number`=:number AND `transaction_date` ORDER BY `transaction_date` DESC limit 1";
What I am trying to do is create a push notification on my App that whenever a new data arrived at my db i will push notify the user on my application.
I am just kind of lost on the comparison of date stuff. Any logic or idea will be very appreciated . Thank you.
Try like this
SELECT `id`, `transfer_amount`, `number`
FROM `transfer_wallet`
WHERE `transfer_number`= :number
AND `transaction_date` >= now()
ORDER BY `transaction_date` DESC limit 1

MySQL query the events for a calendar

I'm building a SaaS calendar with MySQL & PHP.
The front-end javascript calendar lib is fullcalendar(monthly view)
http://fullcalendar.io/
I have a mysql data table named events like this:
ID
ID_USER
BODY_NAME
BODY_START
BODY_END
IS_DONE
TIME_CREATED
TIME_UPDATED
TIME_DELETED
The BODY_START and BODY_END is using unix_timestamp.
Then in my PHP code, I built a query helper that generate a SQL like:
"SELECT * FROM `" . $this->_tableName . "` WHERE `ID_ACCOUNT` = '$_id_account' AND (( `BODY_START` >= 1424649600 AND `BODY_START` <= 1428278400 ) OR (`BODY_END` >= 1424649600 AND `BODY_END` <= 1428278400))";
The start and end timestamp params provided by the fullcalendar.
There is a question:
if the event is crossing the current month, it will not be shown.
example:
ID f8a58d0c8280db2340863a1ac7aa60b0
ID_USER e576c22ce89f38ee422ec818807b99b8
BODY_NAME test_event
BODY_START 1422720000
BODY_END 1428422400
IS_DONE 0
TIME_CREATED 1422845255
TIME_UPDATED 0
TIME_DELETED 0
If you do the query, this event will not show.
how can i fix this? Thanks.
If I understand you correctly, simplify your query to be (in pseudo code):
BODY_START <= month_end and BODY_END >= month_start
Your original query query above requires that events must either start or end during the month, but that's not actually what you want - you want any event that exists for any part of the month.

PHP insert into MySQL after 30min

I have MySQL table:
and I want to add next row (from script in page) with values:
ip: 178.40.12.36
time: 2014-01-22 14:08:04
browser: Google Chrome
browser_version: 32.0.1700.76
platform: windows
country: Slovakia
Question: How to determine in mysql query to insert only if last insert with same identificator (IP+browser+platform) was 30min ago ?
My current insert (pseudo code):
$exist = SELECT *
FROM table
WHERE ip = $ip
AND browser = $browser
AND platform = $platform
if(!$exist) {
INSERT INTO table ...
}
My Idea:
$exist = SELECT ...
...
AND time < $time - 30MIN
Note: How to write this in MySQL?
You may use this as indicator:
SELECT
COUNT(1)
FROM `t`
WHERE `ip` = '$ip'
AND `browser` = '$browser'
AND `platform` = '$platform'
AND `time`>NOW() - INTERVAL 30 MINUTE
-I've replaced time with NOW() for current time, but you may wish to count from your last time value.
It will select records what are newer than 30 minutes, thus, if it's positive, then you don't need to insert new row(s).
Yes, it's easy.
AND time > NOW() - INTERVAL 30 MINUTE
There are many choices like this for date arithmetic.
You could just filter the SELECT for the INSERT:
INSERT INTO `Table` ( ... )
SELECT $ip, $time, $browser, $browser_version, $platform, $country
FROM `Other_Table`
WHERE ip = $ip AND browser = $browser AND platform = $platform AND
time < $time - 30MIN
Now, clearly that syntax won't work exactly, but you get the idea. If the time isn't 30MIN or more ago then it will return 0 records to INSERT.
This will avoid the need of performing a COUNT or EXISTS first; it can be done in one statement.

php code for checking current date is newer than date in database

iam developing a webSite using php and mysql and am really beginner in this area.. Now iam stuck in a place where i have to check the current date is newer than the date in my database
so i have written a code like this but it's not working
$SQL = "UPDATE adverts SET active='0' WHERE enddate<NOW()";
$result = mysqli_query($con,$SQL);
in the above code 'advert' is my table name and 'enddate' is where the column containing date in database
can anybody please help me?
As Anthony described in comments, it's a good idea to check if your query is working in PHPMyAdmin at all, by going to PHPMyAdmin -> SQL -> Run Query. This way you can distinguish if it's an MySQL Error or an PHP Error.
UPDATE adverts SET active = 0 WHERE ( enddate < NOW() )
I've set active '0' to 0, simply because I believe it'll be an Integer field - secondly, there's a small but important difference in your enddate:
is it a date field or a datetime field? See below:
SELECT NOW(); // You will get 2010-12-09 17:10:18
SELECT CURDATE(); // You will get 2010-12-09
Source: MySQL Curdate() vs now()
You can use affected_rows() to see if you query did work, but just didn't meet any criteria
$sql = "UPDATE adverts SET active = 0 WHERE ( enddate < NOW() )";
$queried = mysqli_query( $con, $sql );
if ( mysqli_affected_rows( $con ) >= 1 ) {
//We know some rows were effected.
}
Did you tried
UPDATE adverts SET active='0' WHERE enddate<DATE(NOW())
also, is it adverts or advert?
Or you can try with CURDATE()
UPDATE adverts SET active='0' WHERE enddate<CURDATE()

How to check for existence of specific date in MySQL 'date' column?

Using the code below, I'm having trouble checking whether a specified date exists in a MySQL 'date' column.
$data = array(1367971200);
$s=$dbh->prepare("
SELECT DISTINCT
`date`
FROM
`report_coa_bal_hist`
WHERE
UNIX_TIMESTAMP(`date`) = ?
");
if ($s->execute($data)) {
if ($s['date'] == null) {
return false;
}
return true;
}
It's returning false, despite the fact that I can see the date '2013-05-08' displayed in phpMyAdmin.
The table itself contains 70+ entries for that date. It always will do, if it contains any at all, but I just want to know whether it exists or not at this stage.
The date field is a MySQL 'date' type. I'm suspecting that the bug is in my structuring of the PDO calling of the query.
UPDATE
Updated $r['date'] to `$s['date']. I suspect that I still have an issue with the structure of that, but probably need to fix the query so that it gives us results before focusing on this.
Also tried running the query against the database directly and got an empty resultset, despite being able to see that the target date exists. Still baffled!
Try this
$data = array(1367971200);
$s=$dbh->prepare("
SELECT COUNT(`date`) as c_date
FROM
`report_coa_bal_hist`
WHERE
UNIX_TIMESTAMP(`date`) = ?");
if ($s->execute($data)) {
$result = $s->fetch(PDO::FETCH_ASSOC);
if ($result['c_date'] > 0) {
return false;
}
return true;
}
You can't select a whole day with UNIX timestamps because of their more accurate nature (i.e. seconds), you would need the textual version:
$data = array(date('Y-m-d', 1367971200));
$stmt = $dbh->prepare("SELECT COUNT(*)
FROM `report_coa_bal_hist`
WHERE `date` = ?
");
$stmt->execute($data);
$count = current($stmt->fetchAll(PDO::FETCH_COLUMN, 0));
return $count > 0;
Take note of any timezone differences between the server that runs your script and the database server itself.
there are many flaws with your code, not one:
format of the value you are checking
way you are checking in SQL
the way you are getting result
So, the code have to be
$data = array('2013-05-07');
$sql = "SELECT 1 FROM report_coa_bal_hist WHERE `date` = ? LIMIT 1";
$stm = $dbh->prepare($sql);
$stm->execute($data);
return $stm->fetchColumn();

Categories