PHP - 12hs to 24hs


/ Published in: PHP
Save to your folder(s)

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);


Copy this code and paste it in your HTML
  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

You need to login to post a comment.