We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

nicolaspar on 08/16/07


Tagged

date timestamp convert 12hs 24hs am pm


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

depmed


PHP - 12hs to 24hs


Published in: PHP 


Convert 12hs format to 24hs format. In example: 10:00:00 a.m. return timestamp for 22:00. Or use: strtotime('1970-01-01 '.$hora);


  1. function hours12ToTimestamp24( $hora ){
  2. #Saco solo hora y min en un array
  3. $horaArray = explode(":", $hora);
  4. if( sizeof( $horaArray ) < 2 ) return 0;
  5. #Si tiene pm en el string le sumo 12 hs al mod de la hora sobre 12.
  6. $extra = strstr(strtolower($hora), 'pm') || strstr(strtolower($hora), 'p.m')? 12 : 0;
  7. return mktime(($horaArray[0]%12)+$extra, $horaArray[1], 0, 1, 1, 1970 );
  8. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: koncept on April 22, 2008

function hours12ToTimestamp24($hora) { return date('H:i:s',strtotime($hora)); }

echo hours12ToTimestamp24("10:00:00 pm")

You need to login to post a comment.