Current Location: Home> Latest Articles> Use date_sunset() to automatically switch web wallpapers

Use date_sunset() to automatically switch web wallpapers

M66 2025-05-30

In this article, we will introduce how to use PHP's date_sunset() function to automatically switch web wallpapers based on time. By getting sunset times, we can show different wallpapers on the website to enhance the user experience, especially when switching between day and night.

1. What is the date_sunset() function?

date_sunset() is a built-in function in PHP that gets the sunset time of a specified location. It returns the time of sunset on that day based on the provided latitude and longitude and time.

Function syntax:

 date_sunset(int $timestamp, int $format = SUNFUNCS_RET_TIMESTAMP, float $latitude = ini_get("date.default_latitude"), float $longitude = ini_get("date.default_longitude"));
  • $timestamp : The timestamp of the date and time (if empty, the default is the current time).

  • $format : The returned date format. You can choose SUNFUNCS_RET_TIMESTAMP (timestamp), SUNFUNCS_RET_STRING (character string), etc.

  • $latitude and $longitude : Latitude and longitude, default is the default value in PHP configuration.

2. Ideas for automatically switching wallpapers

What we want to achieve is to automatically switch the wallpaper of the web page according to the sunset time. The specific ideas are as follows:

  • Get the current date and sunset time.

  • If the current time is before sunset, the wallpaper for the day is displayed; if the current time is after sunset, the wallpaper for the night is displayed.

3. Sample code

 <?php
// Get the current date and time stamp
$timestamp = time();

// Set latitude and longitude(Assume it is Beijing)
$latitude = 39.9042;
$longitude = 116.4074;

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

// Get the current time(In timestamp format)
$current_time = time();

// Determine whether the current time is greater than the sunset time,in the case of,Shows the night wallpaper,Otherwise, the wallpapers for the daytime
if ($current_time >= $sunset) {
    $wallpaper_url = 'https://m66.net/images/night_wallpaper.jpg'; // Night wallpaper
} else {
    $wallpaper_url = 'https://m66.net/images/day_wallpaper.jpg'; // Daytime wallpaper
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Automatically switch wallpaper</title>
    <style>
        body {
            background-image: url('<?php echo $wallpaper_url; ?>');
            background-size: cover;
            background-position: center center;
            height: 100vh;
            margin: 0;
        }
    </style>
</head>
<body>
    <h1>Automatically switch web wallpapers</h1>
    <p>According to the current time,Web wallpaper will automatically switch to daytime or night mode。</p>
</body>
</html>

4. Code parsing

  1. Get the current time: Use the time() function to get the current timestamp, stored in the $timestamp variable.

  2. Set latitude and longitude: In the code, we used latitude and longitude of Beijing (39.9042°N, 116.4074°E). You can modify these two values ​​according to your position.

  3. Get the sunset time: Use the date_sunset() function to get the sunset timestamp based on the current time and the specified latitude and longitude.

  4. Judge the time and set the wallpaper: compare the current time and sunset time, and decide whether to use daytime wallpaper or night wallpaper. The if statement here sets $wallpaper_url to a different picture address based on the judgment result.

  5. Web page style: In the HTML section, we set the background image of the web page through the background-image style. According to the value of $wallpaper_url , the background image will be automatically switched.

5. Summary

By using PHP's date_sunset() function, we can easily realize the function of automatically switching web wallpapers based on time. This method is not only simple and efficient, but also very suitable for creating dynamic web pages to improve user experience.