-1

I am fetching Weather information from this API using PHP. My output is like below

enter image description here

I need to convert sunrise and sunset value to human readable format using PHP. In this regard I need to use TimeZone like Asia/Kolkata.

How can I get TimeZone to get sunrise and sunset value to human readable format ?

abu abu
  • 6,599
  • 19
  • 74
  • 131

2 Answers2

1
<?php
   //1638318242
   echo(gmstrftime("%B %d %Y, %X %Z",mktime(16,3,8,31,82,42))."<br>");
   setlocale(LC_ALL,"hu_HU.UTF8");
   echo(gmstrftime("%Y. %B %d. %A. %X %Z"));
?>
 // Output
 September 20 2044, 16:03:08 GMT
 2021. December 04. Saturday. 14:40:27 GMT
Ripon Uddin
  • 709
  • 3
  • 14
  • 29
1

The DateTime class is used to create an object from the timestamp 1638318242. If a DateTime object is created from a timestamp, the time zone is always UTC. With the method setTimeZone() the object is converted into the desired time zone (here Asia/Kolkata). The output can then be brought into the desired form using the format method.

<?php
$sunrise = 1638318242;

$dateTime = date_create('@'.$sunrise)->setTimeZone(new DateTimeZone("Asia/Kolkata"));

echo "Sunrise in Calcutta on ". $dateTime->format('F j, Y \a\t H:i');
//Sunrise in Calcutta on December 1, 2021 at 05:54
jspit
  • 7,276
  • 1
  • 9
  • 17
  • Thanks @jspit. But how can I get `Asia/Kolkata` value from this API response to use like this `new DateTimeZone("Asia/Kolkata")` ? Could you please help me in this regard ? – abu abu Dec 05 '21 at 00:55
  • 1
    If only the seconds offset (timezone=21600) is known for the time zone and no name such as "Asia/Dhaka" is known, you can use the offset to calculate a time zone identifier such as "+0600". – jspit Dec 06 '21 at 09:05
  • 1
    If only the date and time are needed and not a DateTime object, you can just do that: gmdate('F j, Y \a\t H:i', 1638318242+21600); – jspit Dec 06 '21 at 09:11
  • Thanks @jspit. I am fighting with this issue for last 5 days. Finally your `gmdate('F j, Y \a\t H:i', 1638318242+21600);` solution works for me. Thank you very much. Thanks. – abu abu Dec 06 '21 at 11:54