Home >Backend Development >PHP Problem >How to determine whether two timestamps are the same day in php
php method to determine whether two timestamps are one day: 1. Create a php sample file; 2. Set "int time_zone = 8, daily_reset_time = 5;"; 3. Pass "bool IsTimeToReset(time_t last_reset_time, time_t cur_time){...}" method can be used to determine.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How does PHP determine whether two timestamps are the same day?
To determine whether two timestamps are on the same day
Idea analysis
To determine whether two timestamps are not on the same day, just Divide the absolute seconds of each of the two days by 24 * 3600 to get how many days have passed from epoch to now. If the difference between the two results is greater than 0, it indicates that they are not the same day and need to be reset. But two factors need to be considered:
First, the current time minus the set reset time point (such as 5 o'clock every day), because it is reset at 5 o'clock, subtract 5*3600 to ensure the current The time is greater than or equal to the reset time point, which is 5 o'clock on the day.
The second is the absolute seconds plus the part of the timestamp corresponding to the corresponding time zone, so that the time obtained corresponds to the local time. For example, without adding a time zone, you get the number of days since 00:00:00 Greenwich Mean Time on January 1, 1970. Beijing time is 8 hours ahead of Greenwich Mean Time. Then if it is Greenwich Mean Time 16 o'clock in the U.S. time corresponds to zero o'clock on the next day in Beijing time. What we need to calculate in terms of time is the difference in days corresponding to the time in this time zone, so we need to add the time zone. The resulting time is time_zone * 3600
Code implementation
int time_zone = 8, daily_reset_time = 5; time_t BetweenDays(time_t time1, time_t time2) { return (time1 + time_zone *3600)/3600/24 - (time2 + time_zone *3600)/3600/24; } /* * last_reset_time 为上次执行的每日重置时间 * cur_time 为当前时间 */ bool IsTimeToReset(time_t last_reset_time, time_t cur_time) { return BetweenDays(cur_time - 3600 * daily_reset_time, last_reset_time - 3600 * daily_reset_time) > 0; }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine whether two timestamps are the same day in php. For more information, please follow other related articles on the PHP Chinese website!