function hours12ToTimestamp24( $hora ){
#Saco solo hora y min en un array
$horaArray = explode(":", $hora);
if( sizeof( $horaArray ) < 2 ) return 0;
#Si tiene pm en el string le sumo 12 hs al mod de la hora sobre 12.
$extra = strstr(strtolower($hora), 'pm') || strstr(strtolower($hora), 'p.m')? 12 : 0;
return mktime(($horaArray[0]%12)+$extra, $horaArray[1], 0, 1, 1, 1970 );
}
Tag Archive for timestamp
snippets |
January 3, 2012
PHP – 12hs to 24hs
snippets |
December 15, 2011
Translate amount of seconds to hours, minutes, seconds
Duration is a function used to turn seconds into a readable format, measured in weeks, days, hours, minutes and seconds.
Highlight: PHP
<?php
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
Sample usage
Highlight: PHP
<?php
$dateOfBirth = $someTimestamp;
$ageInSeconds = time() - $dateOfBirth;
echo 'I am ' . duration($ageInSeconds) . ' old';
?>
snippets |
October 28, 2011
Format Date
function dater($format = "", $date = "")
{
if($format == "") $format = "Y-m-d H:i:s";
if($date == "") $date = time();
$converted = strtotime($date);
if($converted === false)
return date($format, $date);
else
return date($format, $converted);
}