0

I want to increment Timestamp to generate dates (timestamps being assumed with time=0 hour, 0 minute, 0 second). All worked fine along my loop unless the day of 2016-10-30: Example follows:

    $date1=new DateTime;
    $date1->setDate(2016,10,30);
    $date1->setTime(0,0,0);
    $date1_str=$date1->format('Y-m-d H:i:s');
    $timestamp1=$date1->getTimestamp();
    $timestamp2=$timestamp1+(60*60*24);// create the next day...
    $date2=new DateTime;
    $date2->setTimestamp($timestamp2);
    $date2_str=$date2->format('Y-m-d H:i:s');
    echo "date1_str=$date1_str; date2_str=$date2_str<br>";

The result I get is the following:
date1_str=2016-10-30 00:00:00; date2_str=2016-10-30 23:00:00

The 24 hours increment of timestamp is seen as a 23 hours increment!! The remaing of the loop keep locked to time 23:00:00 instead of 00:00:00

3
  • 3
    No it isn't "bugged".... on the contrary, it's daylight-savings aware; and you've found one of the (typically) two days of the year that don't have 24 hours Commented May 21, 2016 at 19:11
  • 1
    However, $timestamp2=$timestamp1+(60*60*24); suggests that you're assuming that every day of the year has 24 hours Commented May 21, 2016 at 19:13
  • 1
    I just had to mention you can use the methods add and modify on the datetime object instead of what you're doing now. $date = new DateTime('2016-10-30'); $date->modify('+1 days'); does the same as your code. Not saying shorter is always better, but in this case I think it is Commented May 21, 2016 at 21:18

2 Answers 2

1

When you play with time, and especially with dates you have to have in mind DST (Daylight Saving Time) for the date you referred 30/10/2016 the time changes for some countries in the world, so you have to take it into consideration when you apply your time local settings.

Sign up to request clarification or add additional context in comments.

Comments

0

I think that you should use date_default_timezone_set in the top of your instructions. you can see more information in : http://php.net/manual/en/datetime.settimestamp.php.

keep in your mind that you will get varying results dependent on your current timezone. for example when you use date_default_timezone_set('America/New_York'); you will get : date1_str=2016-10-30 00:00:00; date2_str=2016-10-31 00:00:00

i hop this can help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.