Date field update in string format

1

Personal I took an old bank from a client where it has a birth date column that is in the string format and does not date. So far so good so that there are certain dates recorded that do not have 0 in the month or the day when smaller than 10. I need to include the zero where it does not have. Example: Birth = 3/3/1980 need to change to 03/03/1980 how do I do this in the update? Thanks

    
asked by anonymous 13.02.2017 / 16:54

2 answers

1

If there is an id (may be another unique field for each date)

$query = mysql_query("SELECT id,data FROM suatabela");

while($row = mysql_fetch_array($query))
{
    $id= $row['id'];
    $data= $row['data'];

    $partes = explode('/',$data);
    $dia=$partes[0];
    $mes=$partes[1];
    $ano=$partes[2];

    if (strlen($dia)==1){
        $update="true";
        $dia="0".$dia;
    }
    if (strlen($mes)==1){
        $update="true";
        $mes="0".$mes;
    }

    if ($update=="true"){
        $result=$dia."/".$mes."/".$ano;
        mysql_query("UPDATE suatabela SET data='$result' Where id='$id'"); 
    }
}

Note: mysql is discontinued

    
14.02.2017 / 01:25
-1

Make the adjustment fixed, that is:

3/3/1980

If the second character is "/", add 0 in the first position;

03/3/1980

If the third character is "/" add 0 in the third position;

03/03/1980
    
13.02.2017 / 17:46