Avevo bisogno di un elenco di tempi come così in un array ...Come posso creare una serie di tempi con intervalli di mezz'ora?
12am
12:30am
1:00pm
...
Come posso fare questo con PHP?
Avevo bisogno di un elenco di tempi come così in un array ...Come posso creare una serie di tempi con intervalli di mezz'ora?
12am
12:30am
1:00pm
...
Come posso fare questo con PHP?
Grazie per riaprire la domanda alex.
Questa è una soluzione che dovrebbe entrare in risonanza con i programmatori funzionali.
function halfHourTimes() {
$formatter = function ($time) {
if ($time % 3600 == 0) {
return date('ga', $time);
} else {
return date('g:ia', $time);
}
};
$halfHourSteps = range(0, 47*1800, 1800);
return array_map($formatter, $halfHourSteps);
}
ho deciso che questo era meglio :)
function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) {
if ($format === NULL) {
$format = 'g:ia'; // 9:30pm
}
$times = array();
foreach(range($lower, $upper, $step) as $increment) {
$increment = number_format($increment, 2);
list($hour, $minutes) = explode('.', $increment);
$date = new DateTime($hour . ':' . $minutes * .6);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
Questo è forse un modo più elegante, ma richiede i tempi per essere in secondi (che rende anche più flessibile).
function time_range($start, $end, $step = 1800) {
$return = array();
for($time = $start; $time <= $end; $time += $step)
$return[] = date('g:ia', $time);
return $return;
}
Ecco una versione migliorata di Alex's function che utilizza secondi per una maggiore precisione:
function hoursRange($lower = 0, $upper = 86400, $step = 3600, $format = '') {
$times = array();
if (empty($format)) {
$format = 'g:i a';
}
foreach (range($lower, $upper, $step) as $increment) {
$increment = gmdate('H:i', $increment);
list($hour, $minutes) = explode(':', $increment);
$date = new DateTime($hour . ':' . $minutes);
$times[(string) $increment] = $date->format($format);
}
return $times;
}
Così, per fare una serie di volte con intervalli di 1 ora in un periodo di tempo di 24 ore, utilizzare il default:
hoursRange();
che vi darà il seguente:
Array
(
[00:00] => 12:00 am
[01:00] => 1:00 am
[02:00] => 2:00 am
[03:00] => 3:00 am
[04:00] => 4:00 am
[05:00] => 5:00 am
[06:00] => 6:00 am
[07:00] => 7:00 am
[08:00] => 8:00 am
[09:00] => 9:00 am
[10:00] => 10:00 am
[11:00] => 11:00 am
[12:00] => 12:00 pm
[13:00] => 1:00 pm
[14:00] => 2:00 pm
[15:00] => 3:00 pm
[16:00] => 4:00 pm
[17:00] => 5:00 pm
[18:00] => 6:00 pm
[19:00] => 7:00 pm
[20:00] => 8:00 pm
[21:00] => 9:00 pm
[22:00] => 10:00 pm
[23:00] => 11:00 pm
)
Ecco alcuni esempio utilizza:
// Every 15 Minutes, All Day Long
$range = hoursRange(0, 86400, 60 * 15);
// Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format
$range = hoursRange(28800, 61200, 60 * 30, 'h:i a');
È possibile visualizzare un working snippet at CodePad.
Sembra buono - non so esattamente perché stai facendo esplodere il '$ incremento 'piuttosto che passarlo direttamente nel costruttore DateTime? –
più semplice soluzione
$h = 0;
while ($h < 24) {
$key = date('H:i', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$value = date('h:i A', strtotime(date('Y-m-d') . ' + ' . $h . ' hours'));
$formatter[$key] = $value;
$h++;
}
risultati sono:
Array
(
[00:00] => 12:00 AM
[01:00] => 01:00 AM
[02:00] => 02:00 AM
[03:00] => 03:00 AM
[04:00] => 04:00 AM
[05:00] => 05:00 AM
[06:00] => 06:00 AM
[07:00] => 07:00 AM
[08:00] => 08:00 AM
[09:00] => 09:00 AM
[10:00] => 10:00 AM
[11:00] => 11:00 AM
[12:00] => 12:00 PM
[13:00] => 01:00 PM
[14:00] => 02:00 PM
[15:00] => 03:00 PM
[16:00] => 04:00 PM
[17:00] => 05:00 PM
[18:00] => 06:00 PM
[19:00] => 07:00 PM
[20:00] => 08:00 PM
[21:00] => 09:00 PM
[22:00] => 10:00 PM
[23:00] => 11:00 PM
)
Ecco una versione più flessibile che non necessita di DateTime (dato che stiamo già lavorando con timestamp in secondi in ogni caso). ;-)
function get_hours_range($start = 0, $end = 86400, $step = 3600, $format = 'g:i a') {
$times = array();
foreach (range($start, $end, $step) as $timestamp) {
$hour_mins = gmdate('H:i', $timestamp);
if (! empty($format))
$times[$hour_mins] = gmdate($format, $timestamp);
else $times[$hour_mins] = $hour_mins;
}
return $times;
}
Ecco la mia proposta:
$start = new \DateTime('00:00');
$times = 24 * 2; // 24 hours * 30 mins in an hour
for ($i = 0; $i < $times-1; $i++) {
$result[] = $start->add(new \DateInterval('PT30M'))->format('H:i A');
}
print_r($result);
Spero che questo aiuto.
Bella soluzione! Suppongo abbia bisogno di PHP 5.3 con la funzione assegnata a una variabile? +1 (nessuna preoccupazione per la riapertura, se sapessi che sei stato molto impegnato nel risolvere il problema non l'avrei mai cancellato). – alex