simpli Posted April 18, 2010 Share Posted April 18, 2010 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. Thanks in advance, JR Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/ Share on other sites More sharing options...
TeddyKiller Posted April 18, 2010 Share Posted April 18, 2010 $days = (strtotime($date1) - strtotime($date2)) / (60 * 60 * 24); echo $days; Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044123 Share on other sites More sharing options...
simpli Posted April 18, 2010 Author Share Posted April 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044125 Share on other sites More sharing options...
TeddyKiller Posted April 18, 2010 Share Posted April 18, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044128 Share on other sites More sharing options...
simpli Posted April 18, 2010 Author Share Posted April 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044140 Share on other sites More sharing options...
TeddyKiller Posted April 18, 2010 Share Posted April 18, 2010 Ah.. I understand. What you perhaps may need is a method to get the number of days in current year. Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044148 Share on other sites More sharing options...
dotMoe Posted April 19, 2010 Share Posted April 19, 2010 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/198918-timestamp-use/#findComment-1044442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.