array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.date-sun-info.php', 1 => 'date_sun_info', ), 'up' => array ( 0 => 'ref.datetime.php', 1 => 'Funções de Data/Hora', ), 'prev' => array ( 0 => 'function.date-sub.php', 1 => 'date_sub', ), 'next' => array ( 0 => 'function.date-sunrise.php', 1 => 'date_sunrise', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'pt_BR', 'path' => 'reference/datetime/functions/date-sun-info.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.1.2, PHP 7, PHP 8)
date_sun_info — Retorna um array com informações sobre pôr/nascer do sol e o início/fim do dia
timestamp
Timestamp Unix.
latitude
Latitude em graus.
longitude
Longitude em graus.
Retorna um array em caso de sucesso ou false
em caso de falha.
A estrutura do array é detalhada na lista abaixo:
sunrise
sunset
transit
civil_twilight_begin
sunrise
.
civil_twilight_end
sunset
.
nautical_twilight_begin
civil_twilight_begin
.
nautical_twilight_end
civil_twilight_end
.
astronomical_twilight_begin
nautical_twilight_begin
.
astronomical_twilight_end
nautical_twilight_end
.
Os valores dos elementos do array podem ser timestamps UNIX, false
se o
sol estiver abaixo do respectivo ângulo durante todo o dia, or true
se o sol estiver
acima do respectivo ângulo durante todo o dia.
Versão | Descrição |
---|---|
7.2.0 | Os cálculos foram corrigidos com base na meia-noite local em vez do meio-dia local, o que muda os resultados um pouco. |
Exemplo #1 Um exemplo de date_sun_info()
<?php
$sun_info = date_sun_info(strtotime("2006-12-12"), 31.7667, 35.2333);
foreach ($sun_info as $key => $val) {
echo "$key: " . date("H:i:s", $val) . "\n";
}
?>
O exemplo acima produzirá:
sunrise: 05:52:11 sunset: 15:41:21 transit: 10:46:46 civil_twilight_begin: 05:24:08 civil_twilight_end: 16:09:24 nautical_twilight_begin: 04:52:25 nautical_twilight_end: 16:41:06 astronomical_twilight_begin: 04:21:32 astronomical_twilight_end: 17:12:00
Exemplo #2 Noite polar, com algum processamento
<?php
$tz = new \DateTimeZone('America/Anchorage');
$si = date_sun_info(strtotime("2022-12-21"), 70.21, -148.51);
foreach ($si as $key => $value) {
echo
match ($value) {
true => 'always',
false => 'never',
default => date_create("@{$value}")->setTimeZone($tz)->format( 'H:i:s T' ),
},
": {$key}",
"\n";
}
?>
O exemplo acima produzirá:
never: sunrise never: sunset 12:52:18 AKST: transit 10:53:19 AKST: civil_twilight_begin 14:51:17 AKST: civil_twilight_end 09:01:47 AKST: nautical_twilight_begin 16:42:48 AKST: nautical_twilight_end 07:40:47 AKST: astronomical_twilight_begin 18:03:49 AKST: astronomical_twilight_end
Exemplo #3 Sol da meia-noite (Tromsø, Norway)
<?php
$si = date_sun_info(strtotime("2022-06-26"), 69.68, 18.94);
print_r($si);
?>
O exemplo acima produzirá:
Array ( [sunrise] => 1 [sunset] => 1 [transit] => 1656240426 [civil_twilight_begin] => 1 [civil_twilight_end] => 1 [nautical_twilight_begin] => 1 [nautical_twilight_end] => 1 [astronomical_twilight_begin] => 1 [astronomical_twilight_end] => 1 )
Exemplo #4 Calculando a duração do dia (Kiev)
<?php
$si = date_sun_info(strtotime('2022-08-26'), 50.45, 30.52);
$diff = $si['sunset'] - $si['sunrise'];
echo "Duração do dia: ",
floor($diff / 3600), "h ",
floor(($diff % 3600) / 60), "s\n";
?>
O exemplo acima produzirá:
Duração do dia: 13h 56s