How to transform a string representing a date in the format dd/mm/yyyy to another string in the format yyyymmdd ?

Hi Experts!


Could you point a way to transform a string representing a date in the format dd/mm/yyyy to another string in the format
yyyymmdd   ?

I tryed that way with no success
Returning...
700101
700101
(obviously wrong)

It's easy to do by using substring but certainly exists a more affordable way.


<?php
    
    $dt_abertura   = '15/12/2015';
    $dt_fechamento = '20/12/2015';
    
     $dt_abertura     = date('ymd', strtotime($dt_abertura));
     $dt_fechamento   = date('ymd', strtotime($dt_fechamento));
     
     
     echo  $dt_abertura;
     echo "<br/>";
     

     echo  $dt_fechamento;
     echo "<br/>";
     
?>

Open in new window


Thanks in advance!
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel WilsonCommented:
Here are a couple functions I use for Ymd <-> mdY transformations.  They could easily be adapted for dmY transformations.
function Date_mdY_to_Ymd($mdY){	
	$dataDateSql=explode("/",$mdY);
	$dataDateSql=$dataDateSql[2]."-".$dataDateSql[0].'-'.$dataDateSql[1];
	
	return $dataDateSql;
}

function Date_Ymd_to_mdY($Ymd){	
	$dataDateJS=explode("-",$Ymd);
	$dataDateJS=$dataDateJS[1]."/".$dataDateJS[2].'/'.$dataDateJS[0];
	
	return $dataDateJS;
}

Open in new window

Eduardo FuerteDeveloper and AnalystAuthor Commented:
Ok, replied!

Just one observation, to use  Date_Ymd_to_mdY($Ymd)

$dt_fechamento = '2015/12/30';  // Ok?
$dt_fechamento =  Date_Ymd_to_mdY($dt_fechamento);


<?php
    
    $dt_abertura   = '15/12/2015';
    $dt_fechamento = '2015/12/30';
    
    $dt_abertura =  Date_mdY_to_Ymd($dt_abertura);
    $dt_fechamento =  Date_Ymd_to_mdY($dt_fechamento);
    
     echo  $dt_abertura;
     echo "<br/>";
     echo  $dt_fechamento;
     echo "<br/>";


function Date_mdY_to_Ymd($mdY){	
	$dataDateSql=explode("/",$mdY);
	//$dataDateSql=$dataDateSql[2]."-".$dataDateSql[0].'-'.$dataDateSql[1];
    $dataDateSql=$dataDateSql[2].$dataDateSql[0].$dataDateSql[1];
	return $dataDateSql;
}

function Date_Ymd_to_mdY($Ymd){	
	$dataDateJS=explode("-",$Ymd);
	$dataDateJS=$dataDateJS[1]."/".$dataDateJS[2].'/'.$dataDateJS[0];
	return $dataDateJS;
}

     
?>

Open in new window


I'm getting an error

img002
Daniel WilsonCommented:
So you want 20151230 instead of 2015/12/30 ?
function Date_dmY_to_Ymd($mdY){	
	$dataDateSql=explode("/",$mdY);
	//$dataDateSql=$dataDateSql[2]."-".$dataDateSql[0].'-'.$dataDateSql[1];
    $dataDateSql=$dataDateSql[2].$dataDateSql[1].$dataDateSql[0];
	return $dataDateSql;
}

    $dt_fechamento = '20/12/2015';
echo Date_dmY_to_Ymd($dt_fechamento );

Open in new window


How's that?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Thank you, Daniel!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.