php function to invert data

Asked

Viewed 496 times

2

Galera set up a simple function to reverse the date, check this out:

function inverteData($data) {
   if(count(explode("/",$data)) > 1){
      return implode("-",array_reverse(explode("/",$data)));
   }
   else if(count(explode("-",$data)) > 1) {
      return implode("/",array_reverse(explode("-",$data)));
   }
}

Example 1 of how it works:

inverteData("10/02/2006"); 

She returns me 2006-02-10

Example 2 of how it works:

inverteData("2006-02-10"); 

She returns me 10/02/2006

Good need to make it also convert the following date: (200206) to (2006-02-20).

Can someone help me?

  • 2

    You could use the function date(), kind of: date("Y-m-d");, it would take the date "02/10/2006" and sort it in the right way, already this other date format, Voce could do separating the numbers from 2 in 2

  • I think you can help : http://answall.com/questions/139787/howto sort

3 answers

4


One way would be to use the substr:

function inverteData($data) {
    if (count(explode("/", $data)) > 1) {
        return implode("-", array_reverse(explode("/", $data)));
    } elseif (count(explode("-", $data)) > 1) {
        return implode("/", array_reverse(explode("-", $data)));
    }else{
        return '20' . substr($data,4,2) . '-' . substr($data,2,2) . '-' . substr($data,0,2);
    }
}


echo inverteData('200206'); // retorna: 2006-02-20
  • very good it was just that, thank you

2

I would do with operation date, has not if within the method and would be free depending on the settings:

function data_format($format_ini, $value, $format_end)
{
    $d = \DateTime::createFromFormat($format_ini, $value);
    if ($d)
    {
        return $d->format($format_end);
    }
    return null;
}


echo data_format("d/m/Y", "10/02/2006", "Y-m-d");
echo '<br>';
echo data_format("Y-m-d", "2006-02-10", "d/m/Y");
echo '<br>';
echo data_format("dmy", "200206", "Y-m-d");

Example

  • 1

    good implementation (+1).

  • Vlw @Allanandrade

1

Using REGEX, I think it makes everything simpler.

function reverseDate($date, $hours = true){

    $patternBR  = '~(\d{2})/(\d{2})/(\d{2,4})( (\d{2}):(\d{2}):(\d{2}))?~';
    $patternEUA = '~(\d{2,4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2}))?~';

    preg_match($patternBR, $date, $match);
    $isDateBR = !empty($match);

    if(!$isDateBR){
        preg_match($patternEUA, $date, $match);
    }

    if(empty($match)){
        throw new Exception("Error Processing Request", 1);
    }

    $formatBR = '%s/%s/%s';
    $formatEUA = '%s-%s-%s';
    $format = $isDateBR?$formatEUA:$formatBR;

    $date = sprintf($format, $match[3], $match[2], $match[1]);
    if($hours){
        if(isset($match[5])){
            $date .= sprintf(' %s:%s:%s', $match[5], $match[6], $match[7]);
        }else{
            $date .= (' '.date('h:i:s'));
        }
    }
    return $date;
}

echo reverseDate(date('Y-m-d'));    # 15/09/2016 11:20:45
echo reverseDate(date('d/m/Y'));    # 2016-09-15 11:20:45
echo reverseDate(date('y-m-d'));    # 15/09/16 11:20:45
echo reverseDate(date('d/m/y'));    # 16-09-15 11:20:45

Browser other questions tagged

You are not signed in. Login or sign up in order to post.