-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 ?

3
  • 1
    Don't post images of text Dec 4, 2021 at 13:04
  • Thanks @MarcinOrlowski. Actually I showed sample of output.
    – abu abu
    Dec 4, 2021 at 13:06
  • 1
    I'm not willing to retype the numbers in order to run a test, but it's likely to be a Unix time. Half of PHP native date functions accept that as argument. Dec 4, 2021 at 13:15

2 Answers 2

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
4
  • 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 5, 2021 at 0: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 6, 2021 at 9: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 6, 2021 at 9: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 6, 2021 at 11:54
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.