Reply to Re: php function to format date's time

Your name:

Reply:


Posted by ZeldorBlat on 02/24/06 02:58

Juliette wrote:
> laredotornado@zipmail.com wrote:
> > Hi,
> >
> > I am retrieving results from an SQL query, one of which is a date
> >
> > $arrival = $row['itin_arrival_day'];
> >
> > I would like to write a PHP function that formats the date in the
> > following way:
> >
> > If the time part of the date is of the form 2006-05-19 00:00:00 in
> > which the time is midnight, I would like the result to be printed as
> > "05/19/06 ---" but if the time is anything other than midnight, for
> > example, "2006-05-21 18:00:00" I would like the printed result to be
> > "05/21/06 6:00 PM".
> >
> > How can I do this? - Dave
> >
>
>
> Untested and probably not the most efficient, but should work:
>
> $arrival_array = explode( ' ', $arrival );
>
> // format the date
> $date_array = explode( '-', $arrival_array[0] );
> $arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
> $date_array[0] - 2000 );
> unset( $date_array );
>
> // format the time
> if( $arrival_array[1] === '00:00:00' ) {
> $arrival_time = '---';
> }
> else {
> $time_array = explode( ':', $arrival_array[1] );
> if( $time_array[0] > '12' ) {
> $arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
> }
> else {
> $arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
> }
> unset( $time_array );
> }
>
> print $arrival_date . ' ' . $arrival_time;

Alternatively, try something like this:

function foo($str) {
$t = strtotime($str);
return $t == mktime(0, 0, 0, date('m', $t), date('d', $t),
date('Y', $t)) ? date('m/d/y ---', $t) : date('m/d/y g:i', $t);
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация