PX : code

timestamp_convert by Salmon
Download this code


<?php
function timestamp_convert($timestamp$type){
/*-----------------------------------------
         php procedure to convert MySQL TIMESTAMP to a human readable format
         usage is "timestamp_convert($timestamp, $type)"

     $timestamp is the TIMESTAMP field of interest
         $type is one of 'date', 'time', or 'ptime'
         'date' returns "dd.Mmm.yyyy"
             'time' returns standard hh:mm a.m/p.m
         'ptime' returns hh:mm:ss a.m/p.m time
        example use: "echo timestamp_convert($myrow["date"], date);"
                --this will print the date.

     salmon@bluemarble.net
----------------------------------------------------*/

    #if $type == date then a human readable date is calculated from $timestamp
    
if ($type == "date"){
        
$year substr($timestamp04);
        
$day = (int)substr($timestamp62);
        
$monthstamp = (int)substr($timestamp42);
        
$month = array (1=>"Jan""Feb""Mar""Apr""May""Jun""Jul""Aug",
"Sep""Oct""Nov""Dec");
        
#return a formatted date
        
$gooddate $day.".".$month[$monthstamp].".".$year;
        return 
$gooddate;
    }

    
#if $type == time then the TIMESTAMP is converted to a human readable time
    
elseif ($type == "time" || "ptime"){
        
$hour = (int)substr($timestamp82);
        
$minute substr($timestamp102);
        if (
$type == "ptime"){
            
$second substr($timestamp122);
        }

        
#keep the hours out of the military and set am/pm
        
if ($hour <= 11) {
            
$ap "a.m.";
        } else {
            
$hour $hour 12;
            
$ap "p.m.";
        }

        
#no such thing as hour 00 in the human brain, 00 should be 12
        
if ($hour == 0) {
            
$hour 12;
        }
    
        
#return a formatted time
        
if ($type =="time") {
            
$goodtime=$hour.":".$minute." ".$ap;
            return 
$goodtime;
        }
        elseif (
$type == "ptime"){
            
$goodtime=$hour.":".$minute.":".$second." ".$ap;
            return 
$goodtime;
        }
    }
?>


Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.