Current Location: Home> Latest Articles> How to make date_sunset() support international display time

How to make date_sunset() support international display time

M66 2025-06-04

In PHP, the date_sunset() function can calculate the sunset time based on latitude and longitude and date. It returns a UNIX timestamp or a formatted time string. However, this function itself does not directly support multilingual output or different internationalized (i18n) time formats. So, how to make date_sunset() support multilingual and localized display? This article will take you to implement it step by step.

1. Review of basic usage

First, let’s understand the basic usage of date_sunset() :

 <?php
$timestamp = strtotime('2025-04-26');
$latitude = 37.7749;  // For example:San Francisco
$longitude = -122.4194;

$sunset = date_sunset(
    $timestamp,
    SUNFUNCS_RET_TIMESTAMP,
    $latitude,
    $longitude,
    90,
    0
);

echo date('H:i:s', $sunset);
?>

The above code will output sunset time, but the time format is fixed and needs to be formatted manually. And the locale is not considered.

2. Reform to support multilingual and international time formats

To achieve multilingual and internationalization, we can combine PHP's IntlDateFormatter class, which is a powerful tool provided by the intl extension.

First, make sure your server has the intl extension installed. If not, you can refer to the installation here: https://www.m66.net/php-intl-install-guide

Here is a complete improved code example:

 <?php
$timestamp = strtotime('2025-04-26');
$latitude = 37.7749;   // latitude
$longitude = -122.4194; // longitude

// Get the sunset time stamp
$sunset = date_sunset(
    $timestamp,
    SUNFUNCS_RET_TIMESTAMP,
    $latitude,
    $longitude,
    90,
    0
);

// Set language and region,For example法语法国
$locale = 'fr_FR'; // Can be changed to 'zh_CN'、'en_US'、'de_DE' wait

// Set time zone
$timezone = 'Europe/Paris'; // Set the correct time zone as needed

// Create a formatter
$formatter = new IntlDateFormatter(
    $locale,
    IntlDateFormatter::FULL,
    IntlDateFormatter::SHORT,
    $timezone,
    IntlDateFormatter::GREGORIAN
);

// Format sunset time
$localizedSunset = $formatter->format($sunset);

echo "Sunset time(Localization): " . $localizedSunset;
?>

Let's explain:

  • locale controls the output language, such as Chinese, French, German, etc.

  • timezone controls the output time zone to ensure the correct time.

  • IntlDateFormatter can automatically format dates according to language habits, such as turning "26 April 2025" into "26 avril 2025" (French format).

3. Dynamic language switching example

If your website allows users to select languages, such as passing language codes from URL parameters, you can do this:

 <?php
$locale = isset($_GET['lang']) ? $_GET['lang'] : 'en_US';

$formatter = new IntlDateFormatter(
    $locale,
    IntlDateFormatter::FULL,
    IntlDateFormatter::SHORT,
    'UTC',
    IntlDateFormatter::GREGORIAN
);

$sunset = date_sunset(
    strtotime('2025-04-26'),
    SUNFUNCS_RET_TIMESTAMP,
    37.7749,
    -122.4194,
    90,
    0
);

echo $formatter->format($sunset);
?>

If you visit: https://www.m66.net/sunset.php?lang=de_DE , you will see the sunset time in German format.

4. Tips: Customized format

If you want to completely customize the output format, such as only displaying hours and minutes, you can further specify the custom mode:

 $formatter = new IntlDateFormatter(
    'zh_CN',
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    'Asia/Shanghai',
    IntlDateFormatter::GREGORIAN,
    'HH:mm'
);

This setting will output a time format like 18:45 , which is very suitable for mobile or when you need a simple interface.

Summarize

  • date_sunset() itself does not support multilingual and internationalization, but can be used in conjunction with IntlDateFormatter .

  • Remember to set locale and timezone correctly.

  • Allowing users to dynamically switch languages ​​can greatly improve the user experience.

As long as you understand this, similar date-time functions (such as date_sunrise() ) can also be internationalized in the same way.