Link to home
Get AccessLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

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!
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

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

Avatar of Eduardo Fuerte

ASKER

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

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Thank you, Daniel!