Home  >  Article  >  Backend Development  >  PHP date and time application 1: Simple implementation of birthday countdown days

PHP date and time application 1: Simple implementation of birthday countdown days

藏色散人
藏色散人Original
2021-07-28 14:45:263586browse

You must know from the title that I am going to start a new article series~ Then you can click on the previous articles about the PHP mathematical function series ->《PHP mathematical function practice one: max() and min() Fun Analysis of Functions》Start learning, I will continue to update this series~Today’s article will start the PHP date and time journey~

When we develop city-type projects, there are usually Similar to the requirement of event countdown, today we will not talk about that complexity. Let’s learn from the simplest birthday countdown~

Suppose there is such a question->"How do you create a PHP script to implement calculations Number of days between today and birthday ".

Let’s implement it step by step:

First create a PHP sample file demo.php

PHP date and time application 1: Simple implementation of birthday countdown days

The specific code is as follows:

<?php

$target_days = mktime(0,0,0,10,29,2021);// 修改日期 10/29/2021
$today = time();
$diff_days = ($target_days - $today);
$days = (int)($diff_days/86400);
print "距离下一个生日的天数: $days 天!"."<br>";

The birthday time I defined here is October 29, 2021, so let’s run it and see:

PHP date and time application 1: Simple implementation of birthday countdown days

Today is July 28, 2021, when I write this article, and October 29, 2021 is indeed 92 days!

So it’s done, the birthday countdown days are so simple~

For the functions used in the above code, let’s explain them below:

1. # in PHP ##mktime()The function can be used to return the UNIX timestamp of a date. The syntax is "mktime(hour,minute,second,month,day,year,is_dst);", where the parameters Represents hours, minutes, seconds, months, days, and years respectively. The is_dst parameter indicates that if the time is during daylight saving time, it is set to 1, otherwise it is set to 0, and if it is unknown, it is set to -1 (default ). If unknown, PHP will try to find it itself (possibly producing unexpected results). NOTE: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used.

2. The

time() function is used to return the number of seconds of the current time since the Unix epoch (January 1 1970 00:00:00 GMT).

PHP Chinese website has many high-quality PHP video resources. Everyone is welcome to learn for free. Please click on the address ->

https://www.php.cn/course/list/29/type/ 2.html

The above is the detailed content of PHP date and time application 1: Simple implementation of birthday countdown days. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn