DATE and TIME with PDO PHP MYSQL
There are two problems:
Your input string is in an unusual format. Therefore you need to use the DateTime::createFromFormat method to parse it (
strtotime()
can't do the job) using the correct format string.Your output format is missing the time component - you need to add hours (in 24hr format), minutes and seconds to the string.
$dp = DateTime::createFromFormat("m/d/Y - H:i a", $_POST["dp"]);
$dpstr = $dp->format('Y-m-d H:i:s');
$query->bindParam(':dp', $dpstr);
Assuming $_POST["dp"]
contains "08/31/2020 - 05:04 pm"
then $dpstr
will be 2020-08-31 17:04:00
.Demo: http://sandbox.onlinephpfunctions.com/code/b4cc0988c4eee60061502d86f38eccfc97aa9a49
Datetime's NOW() function does not work with PDO-MYSQL prepared statements?
don't pass NOW()
as a parameter,
INSERT INTO timetable(created_on, note) VALUES(NOW(), ?);
so you will only need to bind the notes.$values =array($notes);
PDO::PARAM for dates?
When writing a date in an SQL query, you are writing it as a string; you have to do the same with prepared statements, and use PDO::PARAM_STR
, like you did in the portion of code you proposed.
And for the "timestamp", if by "timestamp" you mean:
- The MySQL timestamp data-type: it's the same, you'll pass it as a
string
- The PHP Unix timestamp, which is an integer: you'll pass it an
int
.
Retrieving today date data from mysql using pdo
You can use this, haven't tested.
<?php
$todaysDate = date('Y-m-d'); //if your date is stored in y-m-d format
try
{
$s = $conn->query("SELECT * from sales where datetime LIKE '%$todaysDate%'");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$results = $s->fetch(PDO::FETCH_OBJ);
?>
Your query results is now in $results
variable.Extended version
<?php
try
{
$s = $conn->query("SELECT * from sales");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
while($results = $s->fetch(PDO::FETCH_OBJ))
{
$date = explode(" ", $results->datetime);
if($date[0] == date('Y-m-d'))
{
//write code here to display data
}
}
?>
Make sure you replace all the columnNames and tablename.Edit :-
Here's a sqlfiddle pertaining to my first solution.
http://sqlfiddle.com/#!9/7c2f1/3
MySQL datetime show 0000 with PHP PDO
Instead of using:
$result->bindParam(2,$timestamp,PDO::PARAM_STR);
Use:$result->bindValue(2,date('Y-m-d H:i:s', $timestamp),PDO::PARAM_STR);
Querying DATE in MySQL using PHP PDO
As I suggested in my comment you can use this code:
$date_start = date('Y-m-d', strtotime($start_date));
$date_end = date('Y-m-d', strtotime($date_end));
To convert your variable contents to a format which MySQL needsNow when you say that you have done it before and it was not working, I assume somewhere between this conversion and binding them to the sql statement
you where changing them in your code, so either you can convert them immediately before binding them or as you suggested just convert them in the bind like this:
$this->bind(':start_date', date('Y-m-d', strtotime($start_date)));
And then your query will look like this$this->sql = "SELECT COUNT(id) AS number_of_items FROM item_table
WHERE id > :id AND date_visited
BETWEEN :start_date
AND :end_date";
So to sum everything up, you can use one of these two ways. either of them should work, but you can use any of them you are more comfortable with:1. Convert the variables before binding them
$date_start = date('Y-m-d', strtotime($start_date));
$date_end = date('Y-m-d', strtotime($date_end));
$this->sql = "SELECT COUNT(id) AS number_of_items FROM item_table
WHERE id > :id AND date_visited
BETWEEN :start_date
AND :end_date";
$this->prepare($this->sql);
$this->bind(':id', 0);
$this->bind(':start_date', $date_start);
$this->bind(':end_date', $date_end);
// rest of your code
2. Convert the variables during binding$this->sql = "SELECT COUNT(id) AS number_of_items FROM item_table
WHERE id > :id AND date_visited
BETWEEN :start_date
AND :end_date";
$this->prepare($this->sql);
$this->bind(':id', 0);
$this->bind(':start_date', date('Y-m-d', strtotime($start_date)));
$this->bind(':end_date', date('Y-m-d', strtotime($date_end)););
// rest of your code
NOW() function in PHP
You can use the date function:
date("Y-m-d H:i:s");
MySQL PDO NOW() as assigned value - is it possible?
No.is it possible?
There are 2 solutions.
- Calculate expiration date using PHP. Something like
date('Y-m-d',strtotime('+1 week'))
Create a conditional part for the query
if(isset($accountStatus)&&$accountStatus!=""){
$tmp[':account_status']=$accountStatus;
if($accountStatus==0){
$accSql = "NOW() + INTERVAL 1 WEEK,";
$tmp[':verified']=0;
}else{
$accSql ='';
$tmp[':verified']=1;
}
$db=$pdo->prepare("UPDATE users SET $accSql $sql WHERE id=:id");
Related Topics
Echo 'String' While Every Long Loop Iteration (Flush() Not Working)
PHP Prepared Statements and Transactions in a Loop
How to Run PHP File Using Cron Jobs
Check If the Current Date Is Between Two Dates + MySQL Select Query
Having Trouble Writing to a File with PHP on Ubuntu
PHP MySQL Charset Utf8 Problems
Doctrine2 Findby Relationship Object Triggers String Conversion Error
Use of Undefined Constant Mcrypt_Rijndael_128 - Assumed 'Mcrypt_Rijndael_128'
How to Pass an Array of Checked/Unchecked Checkbox Values to PHP Email Generator
How to Sort Null Values Last Using Eloquent in Laravel
Docx File Type in PHP Finfo_File Is Application/Zip
How to Refresh Select2 Dropdown Menu After Ajax Loading Different Content
MySQL and Query to Satisfy on Same Column
Difference Between $_Server['Document_Root'] and $_Server['Http_Host']