# Timestamp use

## Recommended Posts

Hi, I have been trying to get the difference in days (or week/month/year) between two dates but I haven't been able to make it work so I'm considering doing it with timestamps. This brings a question to me. If I use a timestamp, am I still going to have to worry about DST (daylight saving times)  and leap years? If so anyone can tell me how I should deal with them.

Any help is wanted I am really stuck and dont know what to do / where to turn to get a coherent way to calculate those differences.

JR

##### Share on other sites

```\$days = (strtotime(\$date1) - strtotime(\$date2)) / (60 * 60 * 24);
echo \$days;```

##### Share on other sites

I understand how to calculate the days but my question was more what happens with DST and leap years. Because obviously if there was a leap year between the two dates i used to calculate the timestamp difference, it's 366 days that's gonna give me a year not 365.

##### Share on other sites

I assume php knows about that already. Why, have you seen a problem related to leapyears? The code I given converts to timestamp. Timestamp is the number of seconds since 1/1/1970. As its the number of seconds (and infact correct) it should know if it was a leap year or not and add/deduct a day?

##### Share on other sites

Hi Teddy,

I dont think it does. The timestamp returns the number of seconds and that is good and does not change whether it's a leap year or not. The problem is if I want to calculate years I have to count 366*24*60*60 and leap years and 365*24*60*60 in non leap years.

JR

##### Share on other sites

Ah.. I understand. What you perhaps may need is a method to get the number of days in current year.

##### Share on other sites

Ah.. I understand. What you perhaps may need is a method to get the number of days in current year.

```<?
\$days = (date("L") == 1) ? 366 : 365;
?>
```

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.