home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / timestampconv.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  2.4 KB  |  67 lines

  1. timestamp_convert 
  2.  
  3. This is a function to convert the MySQL TIMESTAMP type to a human readable format. Use for date, time, or time w/ seconds. I'm not a coding Jedi (yet) so gimme a break, eh? 
  4.  
  5. <?php 
  6. function timestamp_convert($timestamp, $type){ 
  7. /*----------------------------------------- 
  8.          php procedure to convert MySQL TIMESTAMP to a human readable format 
  9.          usage is "timestamp_convert($timestamp, $type)" 
  10.  
  11.      $timestamp is the TIMESTAMP field of interest 
  12.          $type is one of 'date', 'time', or 'ptime' 
  13.          'date' returns "dd.Mmm.yyyy" 
  14.              'time' returns standard hh:mm a.m/p.m 
  15.          'ptime' returns hh:mm:ss a.m/p.m time 
  16.         example use: "echo timestamp_convert($myrow["date"], date);" 
  17.                 --this will print the date. 
  18.  
  19.      salmon@bluemarble.net 
  20. ----------------------------------------------------*/ 
  21.  
  22.      #if $type == date then a human readable date is calculated from $timestamp 
  23.     if ($type ==  "date"){ 
  24.         $year = substr($timestamp, 0, 4); 
  25.         $day = (int)substr($timestamp, 6, 2); 
  26.         $monthstamp = (int)substr($timestamp, 4, 2); 
  27.         $month = array (1=> "Jan",  "Feb",  "Mar",  "Apr",  "May",  "Jun",  "Jul",  "Aug", 
  28. "Sep",  "Oct",  "Nov",  "Dec"); 
  29.          #return a formatted date 
  30.         $gooddate = $day. ".".$month[$monthstamp]. ".".$year; 
  31.         return $gooddate; 
  32.     } 
  33.  
  34.      #if $type == time then the TIMESTAMP is converted to a human readable time 
  35.     elseif ($type ==  "time" ||  "ptime"){ 
  36.         $hour = (int)substr($timestamp, 8, 2); 
  37.         $minute = substr($timestamp, 10, 2); 
  38.         if ($type ==  "ptime"){ 
  39.             $second = substr($timestamp, 12, 2); 
  40.         } 
  41.  
  42.          #keep the hours out of the military and set am/pm 
  43.         if ($hour <= 11) { 
  44.             $ap =  "a.m."; 
  45.         } else { 
  46.             $hour = $hour - 12; 
  47.             $ap =  "p.m."; 
  48.         } 
  49.  
  50.          #no such thing as hour 00 in the human brain, 00 should be 12 
  51.         if ($hour == 0) { 
  52.             $hour = 12; 
  53.         } 
  54.      
  55.          #return a formatted time 
  56.         if ($type == "time") { 
  57.             $goodtime=$hour. ":".$minute. " ".$ap; 
  58.             return $goodtime; 
  59.         } 
  60.         elseif ($type ==  "ptime"){ 
  61.             $goodtime=$hour. ":".$minute. ":".$second. " ".$ap; 
  62.             return $goodtime; 
  63.         } 
  64.     } 
  65. } ?> 
  66.  
  67.