PHP date_create failing when trying to use timestamp from mysql db
Get the solution ↓↓↓i'm just trying to find out why i keep getting this error:
Notice: Undefined variable: flsrfc
now $flsrfc is set like so:
if (date_create($flstart)) {
$flsdt = date_create($flstart);
if ($flsdt) {
$flsrfc = date_format($flsdt, "Y-m-d\TH:i");
} else {
$flsrfc = date_format(date_create(time()), "Y-m-d\TH:i");
}
}
Now the problem i'm having is attempting to set a datetime-local input with the formatted time i'm trying to get. Here is the code for that and where the error says its coming from. My guess is the date_create function isn't working but i have no friggin idea why.
$HTMLOUT .= '<input name="fls" value="'.$flsrfc.'" size="30" type="datetime-local" />';
I made sure $flstart was getting the data from the DB and it is here's the code for it:
$flstart = mysqli_fetch_row(mysqli_query("SELECT value FROM config WHERE name = 'flstart'"))[0];
I've read many of the manual pages and docs for php trying to find out why, i tried strtotime on the timestamp, no go, i also tried forcing it into an integer also same problem. Could someone lease point me in the right direction? Thanks.
Answer
Solution:
Your issue is that ifdate_create
fails,$flsrfc
never gets set. You should rewrite that code as:
if (($flsdt = date_create($flstart)) !== false) {
$flsrfc = date_format($flsdt, "Y-m-d\TH:i");
}
else {
$flsrfc = date("Y-m-d\TH:i");
}
Note that you can simplify theelse
portion by just callingdate
as its default input is the output oftime()
.
In terms of whydate_create
is failing, that would depend on the format of the data in your table. If you are storing it as a MySQLdate
ordatetime
type, there should be no problem with it. Otherwise, you may need to usedate_create_from_format
with an appropriate format parameter to match what is stored in your table.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.