Number of Records Between two datetime [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
It gives me this error when I run the script: Call to a member function fetch_assoc() on boolean. It is driving me crazy.
I get data from input box like this: 2017-12-14 06:42:10
$sql = "SELECT count(*) as total FROM `purchase` where purchase_datetime BETWEEN str_to_date($date1, 'Y-m-d H:i:s') and str_to_date($date2, 'Y-m-d H:i:s')";
or
$sql = "SELECT count(*) as total FROM `purchase` where purchase_datetime BETWEEN '$date1' and '$date2'";
or
Trying This:
$date1 = date('Y-m-d H:i:s', $date1);
$date2 = date('Y-m-d H:i:s', $date2);
FULL CODE:
<?php
if (isset($_POST['submit']) && isset($_POST['date1']) && isset($_POST['date2'])) {
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
$date1 = date('Y-m-d H:i:s', $date1);
$date2 = date('Y-m-d H:i:s', $date2);
$sql = "SELECT count(*) as total FROM `purchase` where purchase_datetime BETWEEN '$date1' and '$date2'";
$query_result = $conn->query($sql);
$rows = $query_result->fetch_assoc();
echo "Result is: " . $rows['total'] . " purchases";
}
?>

The date function is to format today's date, not a given date. Use the DateTime::format function to format a given date. Try this:
$date1 = $date1->format('Y-m-d H:i:s');
$date2 = $date2->format('Y-m-d H:i:s');
$sql = "SELECT COUNT(*) AS total FROM purchase WHERE purchase_datetime BETWEEN '$date1' AND '$date2';";
Alternatively, you can use the date_format alias function like this:
$date1 = date_format($date1, 'Y-m-d H:i:s');
$date2 = date_format($date2, 'Y-m-d H:i:s');
$sql = "SELECT COUNT(*) AS total FROM purchase WHERE purchase_datetime BETWEEN '$date1' AND '$date2';";

make sure your format as:
$date1=$_POST['date1']//1496620800;
$date2=$_POST['date2']//1496707200;
$date1 = date('Y-m-d', $date1);//2017-06-05
$date2 = date('Y-m-d', $date2);//2017-06-06
purchase_datetime format:2017-xx-xx

Related

delete string date on database

I have a table (db_dates) with three columns (id, datetime and name_date).
I want to delete one row, when the date is over, such likes this:
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE '".$rowTime[0]."' < '".$today."'");
}
Not working, how do I do that? It is always deleted all data!
I think this will help you.
Because as per your code date will be always less then today's date the way you are getting.
So you should try below code using DATEDIFF().
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE DATEDIFF('".$rowTime[0]."', NOW()) < 0");
}

Sorting blog posts by date, conversion failed

I am building a blog and im trying to do so that i can sort by year and month, however i get the error:
SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL
Server]Conversion failed when converting date and/or time from
character string.1
Obviously i am using Microsoft SQL server just to make it clear.
i have sorted that when showing the post by doing like this
date_format( new DateTime($postdate['5']), 'd M Y, H:i' );
so how do i implement a thing like that on this piece?
if (isset($_GET['year_month']))
{
$bdate = $_GET['year_month'];
$tsql4 = "SELECT * FROM blog_posts WHERE blog_date=:bdate ORDER BY blogID DESC";
$stmt5 = $conn->prepare($tsql4);
$stmt5->execute (array($bdate));
while($postdate = $stmt5->fetch(PDO::FETCH_BOTH) )
{
here i post the $postdate rows
i have tried some SELECT convert blablabla but havent gotten it to work..
The dates is stored in the db like 2016-01-01 HH:MM:SS
If $postdate['5'] equals to $_GET['year_month'] then you can use this code:
if (isset($_GET['year_month']))
{
// $_GET['year_month'] looks like '2016-02';
list($year, $month) = #explode('-', $_GET['year_month']);
$day = 1;
$datetime = new DateTime();
$datetime->setDate($year, $month , $day);
$datetime->setTime(0, 0, 0);
$bdate_start = date_format($datetime, 'Y-m-d H:m:s');
$datetime->add(new DateInterval('P1M'));
$bdate_finish = date_format($datetime, 'Y-m-d H:m:s');
$tsql4 = "SELECT * FROM blog_posts WHERE blog_date BETWEEN :bdate_start AND :bdate_finish ORDER BY blogID DESC";
$stmt5 = $conn->prepare($tsql4);
$stmt5->execute (array($bdate_start, $bdate_finish));
while($postdate = $stmt5->fetch(PDO::FETCH_BOTH) )
{
here i post the $postdate rows

How to update column with system date using mysql and php

I tried getdate() function, made a string and converted to date and did an update query but it won't work (I'm new to php)
$date = getdate();
$mydate = $date['mon']."/".$date['mday']."/".$date['year'];
$time = strtotime('$mydate');
$newformat = date('Y-d-m');
$sql = "UPDATE product SET p_date =".$newformat. "WHERE p_id = 2";
It won't update, may be the query is wrong, I just want to update table with the system date.
Like you said in php this is the format
Correct format for a MySQL DATETIME column is
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
Try this
$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO table (datePosted) VALUES ('$date')");
Take a look at the manual.Hope this helps.
date_default_timezone_set('Asia/Kolkata'); // set your timezone
$date = date("Y-m-d H:i:s");
$sql = "UPDATE product SET p_date ='$date' WHERE p_id = 2";
the function should be
date() not getdate()
<?php
$date = date('Y-m-d');
$time = date("H:i:s", time());
echo "$date or $time";
?>
Working Version
<?php
$datetime = date('Y-m-d') . " - " . date(" H:i:s", time());
$sql = "UPDATE product SET p_date = $datetime WHERE p_id = 2"";
?>

Selecting new records by datetime

I'm trying to select records which have a recdate within the past year
$goodate = date('Y-m-d h:i:s', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
$sqlstmt = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate.'"' ;
but I'm getting no records.
What am I doing wrong?
$sqlstmt = "SELECT * FROM #__mytable
WHERE id=".$uid."
AND recdate > ADDDATE(CURDATE(), INTERVAL -1 YEAR)";
If you want to do this programmatically, the DateTime object is a great tool.
$date = new DateTime();
$gooddate = $date->sub(DateInterval::createFromDateString('1 year'));
$sql = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate->format('Y-m-d').'"';
This is assuming your date field is mysql type DATE. If not, there are similar DateTime methods for outputting unix timestamps, and the format() method accepts any formatting string that date() does.
$sql = "
select *
from #__mytable
where
id = $uid
and
date_format(recdate, '%Y') = date_format(adddate(curdate(), interval -1 year), '%Y')
"

PHP <<< (multi-line handler?) question

I have some code as follows:
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > date('Y-m-d H:i:s');
QUERY
How can I put the date('Y-m-d H:i:s') in there without breaking out of the <<< statement?
You could store that piece of code in a variable and use substitution.
$now = date('Y-m-d H:i:s');
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > $now;
QUERY;
(Example: http://www.ideone.com/pKSVF)
$date = date('Y-m-d H:i:s');
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > $date
QUERY
http://en.wikipedia.org/wiki/Here_document#PHP

Categories