Is it possible to change a word from one string to another in the database through php?

3

I've been working on a method that will fetch all dates that contain "2014" and replace that value with "2016" without changing the rest of the date. I have already been able to return all the columns that contain "2014" but I do not know how to only change this value in all strings. Thanks for any suggestions.

The code I ran was as follows:

        <?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" You want to stay "2016-01-02 18:00:00"

    
asked by anonymous 12.05.2015 / 12:14

1 answer

2

The field format seems to be of date type (datetime, timestamp etc), in which case use the date manipulation functions as date_add () that adds a date range.

Example

SELECT DATE_ADD(now(), INTERVAL 2 YEAR)

Your code should look something like this:

update voo set data = date_add(data, interval 2 year) where year(data) = '2014'

Example - sqlfiddle

$query = "UPDATE voo SET
             DataPartida = DATE_ADD(DataPartida,INTERVAL 2 YEAR)
          WHERE year(DataPartida) = '2014'";

$result = mysqli_query($connection, $query);

if(!$result) {
   die(mysqli_error($connection);
}
    
12.05.2015 / 13:19