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

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

Related

Number of Records Between two datetime [duplicate]

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

Search data between two dates in dd/mm/yyyy hh:mm:ss in sql

I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh

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");
}

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"";
?>

filter date from timestamp field in mysql

I have saved rows in my table with custom timezone.Now I want to retrieve data from those table for just today.
So here is what I tried
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
And my row contains date in timestamp format like 2015-12-07 22:42:02
But I get empty result.
Try this:
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );
to convert time according to timezone: ConvertTimeZone
if $today='2015-12-07 22:42:02'; your query will give the result.
$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
else do pass the today date and next date and retrieve the value as given
$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next' " );
for more details refer this Oracle SQL : timestamps in where clause How to compare Timestamp in where clause
You should convert date to timestamp before passing it to mysql:
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );

Categories