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

Eduardo Fuerte
Eduardo Fuerte used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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 Analyst

Author

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
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?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Thank you, Daniel!
Most Valuable Expert 2011
Top Expert 2016

Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial