3
I have been working on a method that will fetch all dates containing "2014" and replace that value with "2016" without changing the rest of the date. I’ve been able to return all columns containing "2014" but I don’t know how to change only this value in all strings. I appreciate any suggestion.
The code I executed was the following:
<?php
$query = "SELECT * ";
$query .= "FROM voo ";
$result = mysqli_query($connection, $query);
if(!$result) {
die ("Database query failed.");
}
//while($row = mysqli_fetch_row($result)){
// if (strpos($result,'2014') !== false) {
$query = "UPDATE voo SET DataPartida = DATE_ADD(DataPartida, ";
$query .= "INTERVAL 2 YEAR) WHERE year(DataPartida) = '2014'";
var_dump($row);
//}
echo "<hr />";
//}
?>
Example: "2014-01-02 18:00:00" Intended to stay "2016-01-02 18:00:00"
Could you do something with
regex
.– MeuChapeu
I looked for this method (PREG_REPLACE_EVAL), and it was just something I was looking for, but php.net says it will no longer be compatible with the browsers: http://php.net/manual/en/reference.pcre.pattern.modifiers.php
– angelfmf
@angelfmf the function is not obsolete, what is obsolete is the modifier
e
. "The modifier/e
is obsolete. Use preg_replace_callback() instead. See documentationPREG_REPLACE_EVAL
for additional information on security risks." And there is no why the browser does not support, because the function runs on the server and not on the browser.– MeuChapeu
@angelfmf The field in the database is of what type? By far, regex is the worst option in this case.
– gmsantos
if the data is in a format suitable for the datetime type, modify the column for the datetime type, then you can apply a single query to update the data. There is no need to use php... but if you want to query php there is also no problem. But it’s completely unnecessary to loop all the records and make the replacements with php.
– Daniel Omine
The Column is already type datetime, I checked in the database.
– angelfmf