How to make a full date common?

Asked

Viewed 733 times

5

I receive a string with a date, and unfortunately there is no way to change it, in the format:

1 de September de 2015

And make it into:

01/09/2015

The only way I could do this would be to break the string and form an array, and check the required parts. But there is some other way?

I know there’s no way to use strtotime and strftime.

  • You’re gonna have to do a job, buddy. I can sense the geek

2 answers

4


One way to solve this is to replace the month me portugues by in English through an array with the help of str_replace().

<?php

$en = ['','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$pt = [' de ', 'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro']; 

$data = '1 de setembro de 2015';
$data = str_replace($pt, $en, $data);
echo date('Y-m-d', strtotime($data));
  • @Jjoao pq ' ' ? in the case of de be pasted? it already n will convert the date correctly.

  • 1

    @Jjoao, correct, at the time of convter December he replaced the de for nothing, I added your correction. thanks :D

1

I’d do it this way:

function arruma_data($data){
    $arr_mes = array("Setembro"=>"09","Outubro"=>"10","Novembro"=>"11");
    $arr_data = explode(" de ",data)
    $datafim = str_pad($arr_data [0],2,"0",STR_PAD_LEFT)."/".$arr_mes[$arr_data[1]]."/". $arr_data[2];
    return $datafim;
}

echo $this->arrumada_data("1 de setembro de 2015");

Browser other questions tagged

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