I also like to modify some datetime fields on afterfind() callbacks
but I believe it's also good to create your own helper where you would
have methods to show dates any way you want. For example I created
this one which allows to output datetime fields in many different
ways.

class DataHelper extends AppHelper {

    function formata($data, $formato = 'completa')
        {
        $hora = substr($data,11,8);
        if(!empty($hora)){
            list ($h,$m,$s) = explode (':', $hora);
            $hora = $h.':'.$m;
        }

        $data = substr($data,0,10);
        list ($ano,$mes,$dia) = explode ('-', $data);

        $diaSemana = array();
        $diaSemana["Sun"] = "Dom";
        $diaSemana["Mon"] = "Seg";
        $diaSemana["Tue"] = "Ter";
        $diaSemana["Wed"] = "Qua";
        $diaSemana["Thu"] = "Qui";
        $diaSemana["Fri"] = "Sex";
        $diaSemana["Sat"] = "Sáb";

        $mesAno = array();
        $mesAno['01'] = "Jan";
        $mesAno['02'] = "Fev";
        $mesAno['03'] = "Mar";
        $mesAno['04'] = "Abr";
        $mesAno['05'] = "Mai";
        $mesAno['06'] = "Jun";
        $mesAno['07'] = "Jul";
        $mesAno['08'] = "Ago";
        $mesAno['09'] = "Set";
        $mesAno['10'] = "Out";
        $mesAno['11'] = "Nov";
        $mesAno['12'] = "Dez";

        $mesLongo = array();
        $mesLongo['01'] = "Janeiro";
        $mesLongo['02'] = "Fevereiro";
        $mesLongo['03'] = "Março";
        $mesLongo['04'] = "Abril";
        $mesLongo['05'] = "Maio";
        $mesLongo['06'] = "Junho";
        $mesLongo['07'] = "Julho";
        $mesLongo['08'] = "Agosto";
        $mesLongo['09'] = "Setembro";
        $mesLongo['10'] = "Outubro";
        $mesLongo['11'] = "Novembro";
        $mesLongo['12'] = "Dezembro";

        $diaDaSemanaTimeStamp = mktime(0,0,0,$mes,$dia,$ano);
        $diaDaSemana = date('D',$diaDaSemanaTimeStamp);

        if($formato === 'completa')
        {
            $dataFormatada = $dia.'-'.$mes.'-'.$ano.', '.$hora;
        }
        else if($formato === 'diamesano')
        {
            $dataFormatada = $dia.'-'.$mes.'-'.$ano;
        }
        else if($formato === 'porextenso')
        {
            $dataFormatada = $dia.'-'.$mesAno[$mes].'-'.$ano;

        }else if ( $formato === 'mesextenso' ){
            # data já esta no formato 'humano'
            list ($dia,$mes,$ano) = explode ('-', $data);
            $mesExtenso = $mesAno[$mes];
            $dataFormatada = $dia.'-'.$mesExtenso.'-'.$ano;
        }else if ( $formato === 'diadasemana' ){
            $dataFormatada = $diaSemana[$diaDaSemana];
        }else if ( $formato === 'longadescricao' ){
            $dataFormatada = $diaSemana[$diaDaSemana].', '.$dia.' '.
$mesLongo[$mes].', '.$ano;
        }

        return $dataFormatada;
        }

}

On 12 dez, 15:11, Somchok Sakjiraphong <[email protected]> wrote:
> I usually use the afterFind() technique described in the 
> Cookbookhttp://book.cakephp.org/view/1048/Callback-Methods
>
> On Dec 12, 12:02 am, cricket <[email protected]> wrote:
>
>
>
> > echo date('M j, Y', strtotime($post['Post']['created']));
>
> > What I've done in the past a couple of times is put code in
> > afterFind() to add something like this as
> > $data[$this->alias]['created_string'].
>
> > On Fri, Dec 10, 2010 at 11:41 AM, georgeman <[email protected]> 
> > wrote:
> > > What if I want to only show the date and not the time, for example:
> > > December 10, 2010
>
> > > I have a solution that works but it seems to me there must be an
> > > easier way to do, something that I overlooked. Here is my solution:
>
> > > <?php echo date('M j, Y',($this->Time->fromString($post['Post']
> > > ['created']))); ?>
>
> > > Is there an easier way to do this?
>
> > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others 
> > > with their CakePHP related questions.
>
> > > You received this message because you are subscribed to the Google Groups 
> > > "CakePHP" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected] For more options, visit this group 
> > > athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to