| 
	
 | 
 Posted by laredotornado on 02/24/06 06:06 
Thanks for this neat, concise function.  In it, how would you add 
"AM/PM" to the times that are not midnight? 
 
Thanks, - Dave 
 
ZeldorBlat wrote: 
> 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); 
> }
 
  
Navigation:
[Reply to this message] 
 |