Home > Backend Development > PHP Tutorial > 1 day equals 86400 seconds?

1 day equals 86400 seconds?

WBOY
Release: 2016-07-30 13:30:15
Original
3279 people have browsed it

Date operations are often used when writing code. Most of the time, for convenience, it is possible to simply add and subtract a timestamp:

$time = mktime(0, 0, 0, 8, 28, 2015);
$one_hour_later = date('Y-m-d H:i:s', $time + 3600);
Copy after login

This is a simple code to get the timestamp one hour later, but if you zoom in to the day When using the above units, is this timestamp really what you want?

For example, if you have a timestamp of 0 o'clock on a certain day and want to know the timestamp of 0 o'clock 5 days later, simply add the time after 5*86400, right?

$time = mktime(0, 0, 0, 3, 8, 2015);
$five_day_later = date('Y-m-d H:i:s', $time + 5 * 86400);
echo $five_day_later; 
Copy after login

>> 2015-03-13 00:00:00

It seems there is no problem. But what about people in another place executing it?

// 时区设置为美国芝加哥
date_default_timezone_set('America/Chicago');
$time = mktime(0, 0, 0, 3, 8, 2015);
$five_day_later = date('Y-m-d H:i:s', $time + 5 * 86400);
echo $five_day_later; 
Copy after login
Copy after login

>> 2015-03-13 01:00:00

Why isn’t it 0 o’clock 5 days later?

Of course, this key problem lies in daylight saving time (the specific principle of daylight saving time will not be introduced, you can Baidu yourself).

Solution: Use the strtotime function instead of addition and subtraction

// 时区设置为美国芝加哥
date_default_timezone_set('America/Chicago');
$time = mktime(0, 0, 0, 3, 8, 2015);
$five_day_later = date('Y-m-d H:i:s', strtotime('+5 days', $time));
echo $five_day_later;
Copy after login
>> 2015-03-13 00:00:00

Although this problem does not exist in those time zones that do not have daylight saving time, but for those with daylight saving time The time zone that exists at the time does have an impact on your expected results.

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces how 1 day is equal to 86400 seconds? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template