我创建了一个php函数,以数字时钟格式返回时间。我的功能是
function get_hh_mm_ii($unsorted_time) {
//return $unsorted_time;
if ($unsorted_time == '00:00') {
return 'x';
} elseif ($unsorted_time != '00:00') {
$time = explode(":", $unsorted_time);
$hh = $time[0];
$mm = $time[1];
//var_dump($hh/12); exit();
if (($hh / 12) == 0) {
$ii = 'am';
return $hh . ':' . $mm . ' ' . $ii;
} elseif (($hh / 12) == 1) {
$ii = 'pm';
$hh = $hh % 12;
if ($hh == 0) {
$hh == 12;
}
return $hh . ':' . $mm . ' ' . $ii;
}
}
}
当我试图传递值(02:03)或任何其他值时,它总是返回NULL。我使用var_dump检查了它的值。
试试这个:
function get_hh_mm_ii($unsorted_time) {
//return $unsorted_time;
if ($unsorted_time == '00:00') {
return 'x';
} elseif ($unsorted_time != '00:00') {
$time = explode(":", $unsorted_time);
$hh = $time[0];
$mm = $time[1];
//var_dump($hh/12); exit();
if ($hh < 12) {
$ii = 'am';
return $hh . ':' . $mm . ' ' . $ii;
} else {
$ii = 'pm';
$hh = $hh - 12;
if ($hh == 0) {
$hh == 12;
}
return $hh . ':' . $mm . ' ' . $ii;
}
}
}
问题是,除了12除以12之外,13和其他任何东西都不是一。
我只想做:
function get_hh_mm_ii($unsorted_time) {
return date('g:i a', strtotime($unsorted_time));
}
也许我误解了这个问题,但您只需要组合使用strtotime
和date
。
<?php
function get_hh_mm_ii($unsorted_time) {
return date("G':H A", strtotime($unsorted_time));
}
print get_hh_mm_ii("9.21 pm") . PHP_EOL;
print get_hh_mm_ii("9:30 AM") . PHP_EOL;
print get_hh_mm_ii("02:03") . PHP_EOL;
?>
输出:
$ php timetest.php
21:21 PM
9:09 AM
2:02 AM
function startClock() {
let today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
var d = today.getDay();
var day = today.getDay();
var dayarr = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
day = dayarr[day];
let ampm = h >= 12 ? 'PM' : 'AM';
h = h % 12;
h = h ? h : 12; // the hour '0' should be '12'
m = m < 10 ? '0' + m : m;
h = h < 10 ? '0' + h : h;
s = s < 10 ? '0' + s : s;
document.getElementById('ets-clock').innerHTML =
day + " " + h + ":" + m + " " + ampm;
let t = setTimeout(startTime, 500);
}
<body onload="startClock()">
<div id="ets-clock" class="ets-clock ets-time" ></div>
</body>