1 days equals 86,400 seconds?

Source: Internet
Author: User
Tags time zones
Date arithmetic is often used in writing code. Most of the time, for convenience, it is possible to simply add and subtract a timestamp:

$time = mktime (0, 0, 0, 8, _hour_later); $one = Date (' y-m-d h:i:s ', $time + 3600);

This is a simple code to get a timestamp after an hour, but if you zoom in to a unit above the day, is this timestamp really what you want?

For example, if you have a time stamp of 0 o ' Day, you want to know the time stamp of 0 o ' day after 5 days, simply add the time after 5*86400?

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

There seems to be no problem. But what about the people in a different place?

The time zone is set to Chicago Date_default_timezone_set (' America/chicago '); $time = mktime (0, 0, 0, 3, 8); $five _day_later = Date (' Y-m-d h:i:s ', $time + 5 * 86400); echo $five _day_later;

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

Why isn't it 0 o ' day after 5 days?

Of course, this key problem is in daylight saving time (the principle of specific daylight savings is not introduced, can be self-Baidu).

Workaround: Use the Strtotime function instead of adding and subtracting

The time zone is set to Chicago Date_default_timezone_set (' America/chicago '); $time = mktime (0, 0, 0, 3, 8); $five _day_later = Date (' Y-m-d h:i:s ', Strtotime (' +5 days ', $time)); Echo $five _day_later;
>> 2015-03-13 00:00:00

While this problem does not exist in time zones that do not have daylight savings, it does affect your expected results for time zones that have daylight savings.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above is about 1 days equals 86,400 seconds? , including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.