Skip to content

Instantly share code, notes, and snippets.

@MilkZoft
Created January 18, 2012 03:45
Show Gist options
  • Select an option

  • Save MilkZoft/1630761 to your computer and use it in GitHub Desktop.

Select an option

Save MilkZoft/1630761 to your computer and use it in GitHub Desktop.
codejobs - Get hour from a date - PHP
<?php
function getHour($date) {
$date = explode(" ", $date);
if(count($date) > 1) {
$time = explode(":", $date[1]);
} else {
$time = explode(":", $date[0]);
}
$hours = (int) $time[0];
$minutes = $time[1];
if($hours > 12) {
$day = "P.M.";
if($hours === 13) {
$hour = "01";
} elseif($hours === 14) {
$hour = "02";
} elseif($hours === 15) {
$hour = "03";
} elseif($hours === 16) {
$hour = "04";
} elseif($hours === 17) {
$hour = "05";
} elseif($hours === 18) {
$hour = "06";
} elseif($hours === 19) {
$hour = "07";
} elseif($hours === 20) {
$hour = "08";
} elseif($hours === 21) {
$hour = "09";
} elseif($hours === 22) {
$hour = "10";
} elseif($hours === 23) {
$hour = "11";
} elseif($hours === 00) {
$hour = "12";
}
} else {
$day = "A.M.";
$hour = $hours;
}
return "$hour : $minutes $day";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment