How do you use bind_params for a timestamp data? - php

I am trying to get data on a database between two timestamps and I have seen many posts that use the BETWEEN. But I can't get past the error that is related to my bindparams that's why I can't test the codes that I saw on another post.
About the first part of my code, I just thought that the value on $date1 and $date2 is a string and I thought that maybe I need it to be converted to a MySQL timestamp format so I used the following code.
How do I use bindparams on a timestamp based on my code?
I tried the following code but I keep getting an error on POSTMAN.
such as Number of variables doesn't match the number of parameters
This is the sample value for date1 and date 2 that I am using on Postman for testing
$date1 is 2019-12-13 00:00:00
$date2 is 2019-12-15 00:00:00
DbOperation.php
//view cart product sales in cartData via date
public function viewCartDatabyDate($date1, $date2){
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$newDate1=date("Y-m-d H:i:s", $timestamp1);
$newDate2=date("Y-m-d H:i:s", $timestamp2);
$stmt = $this->con->prepare("SELECT * FROM cart_data WHERE created BETWEEN '$newDate1' AND '$newDate2' ORDER BY created");
//$stmt->bind_param("s", $newDate1);
//$stmt->bind_param("s", $newDate2);
$stmt->bind_param("s", $created);
$result = $stmt->get_result();
while($data = $result->fetch_assoc()){
$cartdata[]=$data;
}
return $cartdata;
}

You need to use placeholders. Question marks in SQL mean that this is the place where the variable is attached.
$stmt = $this->con->prepare("SELECT * FROM cart_data WHERE created BETWEEN ? AND ? ORDER BY created");
$stmt->bind_param("ss", $newDate1, $newDate2);
$result = $stmt->get_result();
$certdata = $result->fetch_all(MYSQLI_ASSOC);

Related

CodeIgniter Model Date Range

I have been struggling for a few weeks. Recently I posted a question here CodeIgniter Datatables Server Side Date Range using Datepicker but no luck.
Then I decide to isolate the problem by testing the Codeigniter Model and seems like there is a problem there.
Below is the code and image.
Codeigniter Model
function get_allbillinglistByDate($startdate,$enddate){
$data = array();
$sdate = "09/01/2020";
$edate = "11/01/2020";
$this->db->from('invoices');
$multipleWhere = ['invoices.Approved' => 1,'invoices.xero' => 0];
$this->db->where($multipleWhere);
//$this->db->where('Invoice_Date BETWEEN "'. date('m-d-Y', strtotime($sdate)). '" and "'. date('m-d-Y', strtotime($edate)).'"');
$this->db->where('Invoice_Date >=', date('m-d-Y', strtotime($sdate)));
$this->db->where('Invoice_Date <=', date('m-d-Y', strtotime($edate)));
$Q = $this->db->get();
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Here is the screenshot
Date Column where only 2020 record should show but it shows 2021 too
Not sure where things are going wrong the date Column where only 2020 record should show but it shows 2021 too
Please advise.
Although MySQL tries to interpret values in several formats, date parts must always be given in year-month-day order (for example, '98-09-04'), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98', '04-09-98'). To convert strings in other orders to year-month-day order, the STR_TO_DATE() function may be useful.
Ref: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html
So, change your code as below
date('Y-m-d', strtotime($sdate)
The answer of Raj is correct. You can tweak it according to your expected format like below
date('d/m/Y', strtotime($sdate));
date('d/m/Y', strtotime($edate));
An alternative syntax of your method but you may need to tweak it a little. You should format date params and then send them to this method, you can format them as we described
function get_allbillinglistByDate($startdate,$enddate){
$sql = "SELECT * FROM invoices where Approved = 1 AND xero = 0 AND DATE(Invoice_Date) BETWEEN ? AND ?";
$data = array();
array_push($data, $startdate);
array_push($data, $enddate);
$query = $this->db->query($sql, $dataArr);
return (array)$query->result_array();
}
For more info check https://www.php.net/manual/en/function.date.php
ok guys the problem is resolved. I had to change the column type in my MYSQL table in PHPMyAdmin from varchar to date in order to get the date range.

Adding DateTime intervals with foreach loop

I asked a question yesterday in adding together datetime intervals and was pointed to this thread - How we can add two date intervals in PHP
This is great and makes sense. However, when I try to do what the submitted answer says in a foreach loop, I'm ending up with an incorrect result.
This is a function I have made that gets all the clock in times and out times of staff, they are stored in the db and are created using PHP's date(H:i:s).
My function gets all the in and out times of any given employee, and my reporting feature I'm working on needs to display the total amount of hours they have worked.
I tried to achieve this by converting the times to datetime objects and using ->diff to get the intervals and thus calculating that days hours, I am then trying use a foreach loop to add the intervals together thus giving me a sum total of the hours worked in any given date range.
The whole function together is this:
function getTotalHours($staff_id,$from_date,$to_date){
$sql = "SELECT * FROM oc_staff_times WHERE date BETWEEN '$from_date' AND '$to_date' AND staff_id = '$staff_id'";
$result = $this->conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$base_time = new DateTime('00:00');
$total = new DateTime('00:00');
foreach ($data as $time) {
$in = new DateTime($time['clock_in_time']);
$out = new DateTime($time['clock_out_time']);
$interval = $in->diff($out);
$base_time->add($interval);
}
return $total->diff($base_time)->format("%H:%I");
}
}
I was hoping to get a monthly total, however it seems I'm only getting one days total as my final result. Here is a screen shot of the UI (the total hours are crudely circled) this also shows the time stamps my function should be adding together.
You can do this in a single query instead. Use TIMEDIFF() to get the difference for each row, then convert those to seconds by using TIME_TO_SEC(), SUM() those up and put it back into time-format with SEC_TO_TIME() - all in MySQL!
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`clock_out_time`, `clock_in_time`))))
FROM `oc_staff_times`
WHERE `staff_id` = ?
AND `date` BETWEEN ? AND ?
Making your function with a prepared statement..
function getTotalHours($staff_id, $from_date, $to_date){
$sql = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`clock_out_time`, `clock_in_time`))))
FROM `oc_staff_times`
WHERE `staff_id` = ?
AND `date` BETWEEN ? AND ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("sss", $staff_id, $from_date, $to_date);
$stmt->execute();
$stmt->bind_result($totalTime);
$stmt->fetch();
$stmt->close();
return $totalTime;
}
SQL fiddle showing the query in action http://sqlfiddle.com/#!9/525b83/7
The answer from Qirel offers a nice way to do this in SQL, however if you want to understand why your code didn't work:
$base_time = new DateTime('00:00'); does not create an interval, it's a date. It means that if you add 24 hours to it and ask only the time part, it will show '00:00' because you end up the day after.
One solution would have been to declare $base_time as an interval, for example like this:
$base_time = new DateInterval('PT0S');
And at the end output directly like this:
$base_time->format("%H:%I");

PHP/SQL Converting timestamp

So I am working on a simple website and I ran into a problem. I have a subscription based website and I have a date expired for when their subscription ends. This all works well, but when I tried to display the expiration date I ran into problems. The first 3 lines are what i have been trying. It seems as if the timestamp isnt correctly being transferred from the database because when I did my test at the button, this displayed the correct date. The top 3 lines always give me this: 1970/01/01
// Get Expiration Date
// Always gives me 1970/01/01
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$timestamp = mysqli_query($link, $datexpire);
$date = date("Y/m/d",$timestamp);
//This works
$timestamp2 = 1537847863;
$date2 = date("Y/m/d",$timestamp2);
If anyone could help that would be much appreciated
i think your code should be something like
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row=mysqli_fetch_assoc($result));
$timestamp = $row['date_expire'];
$date = date("Y/m/d", $timestamp);
echo $date;
please check for column name.. if it is date-expire or date_expire ??? (dash or underscore ??)
mysqli_query returns a mysqli_result object or a boolean value.
You want to fetch a row from your given object, like so:
$datexpire = "SELECT `date-expire` FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row = mysqli_fetch_assoc($result);
$date = date("Y/m/d", $row["date-expire"]);

mysqli prepared statements with between clause

My question is similar to [this][1] but the solution provided there doesn't work for me.
I have a DB and I have one column called birth that contains a date in the format year-day-month 00:00:00. Now I want to extract from the DB all the records which birth is between two dates.
The birth value has been inserted in the DB with this code
$date = $day . "-" . $month. "-" . $year;
$a = strptime($date, '%d-%m-%Y');
$DB_date = date('Y-m-d H:i:s',mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900));
To extract the dates I am using this code:
$stmt = $conn->prepare('SELECT * FROM database WHERE `birth` BETWEEN ? AND ?');
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
$stmt->bind_param('ss',$start_date,$stop_date);
$stmt->execute();
$result = $stmt->get_result();
but it doesn't find anything so $result->num_rows is equal to 0 and it doesn't give any errors so the problem must be in the comparison of the dates.
Birth column format is y-d-m hour min sec. But in your where clause you are ignoring hour minute second factor.
Replace:
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
With this:
$start_date = '1990-01-01 00:00:00';
$stop_date = '1995-01-01 00:00:00';
Kindly try it and let me know if it works
I found the solution
The code in my question works if the data type of the column in the DB is datetime and you also need to make sure that $start_date is older than $stop_date. I still wonder though why it didn't work with the date data type.
I had the same issue. I found another thread with the same problem (not sure how to duplicate this thread) here
However just to sum up,
They managed to get the desired results by bracketing the question marks for the params.
$stmt = $conn->prepare('SELECT * FROM database WHERE `birth` BETWEEN (?) AND (?)');
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
$stmt->bind_param('ss',$start_date,$stop_date);
$stmt->execute();
$result = $stmt->get_result();
Hope this helps.

Failing to compare php date with mysql date

Got stuck in a complex or maybe stupid problem. I am getting a query from mysql, and then trying to compare a date column with a PHP data which i formatted to the same format i.e "Y-m-d" it always results in no match, although i see there is a match.. and it gets the right result set too.
<?php
date_default_timezone_set('America/Los_Angeles'); // set timezone to our timezone
$constantTime = time(); // get value of time in constant
$appDate = date("Y-m-d", $constantTime); //that defines php time variable -
$queryDate = "SELECT * FROM date WHERE date='$appDate'";
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resulDate);
if ($appDate == date("Y-m-d", strtotime($recordDate['date']))) {
echo "MATCH ";
$dateID = $recordDate['dateID'];
} else {
mysql_query("insert into date(date) values('$appDate')")or die("Database write error1");
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resultDate);
echo "NO MATCH ";
$dateID = $recordDate['dateID'];
}
This is always triggering the else, i tried === instead of ==, i tried strcmp
As i assume you're comparing datetime field, you have two possibilities:
Cast field to date:
$queryDate = "SELECT * FROM your_table WHERE date(your_date_field) = date('$appDate')";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
or
Modify your date format to be ISO compatible:
$appDate = date("Y-m-d H:i:s", $constantTime); //it defines date in format 2015-03-14 15:00:00
$queryDate = "SELECT * FROM your_table WHERE your_date_field='$appDate'";
See also this question

Categories