davidkierz Posted October 12, 2008 Share Posted October 12, 2008 supposed i have 2 dates $date1 and $date2 they are in this format "2008-10-09" how would i word the code to figure out the number of days between them like $date2 - date1 = 15 days ? Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/ Share on other sites More sharing options...
xtopolis Posted October 12, 2008 Share Posted October 12, 2008 <?php echo date("d",strtotime("2008-10-09") - strtotime("2008-09-26")); ?> //13 Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663582 Share on other sites More sharing options...
davidkierz Posted October 12, 2008 Author Share Posted October 12, 2008 THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663593 Share on other sites More sharing options...
ibechane Posted October 12, 2008 Share Posted October 12, 2008 First of all, there is a unit and reference for dates and times called a "time stamp." A time stamp is simply the number of seconds since a reference point. On Unix systems, this reference point is January 1, 1970. In PHP, there is a function called "time()" that returns a Unix time stamp of "now," the time at which it's called. For example, I just ran time() a moment ago and got 1223854877. In other words, there have been about 1.2 billion seconds since 1/1/1970. Now that you know what a time stamp is, you can use the function mktime() function give you the time stamp of a given date. For example, here is how you get the time stamp of February 23, 1999, 11:35:00 AM: <?php echo mktime(11,35,00,2,23,1999); /* returns "919787700" which is the no. of seconds between Jan 1, 1970 midnight and Feb 23,1999, 11:35:00 AM */ ?> From there, all you have to do is find the time stamp of both dates, and convert seconds into days (I assume you know how to do division). Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663597 Share on other sites More sharing options...
ibechane Posted October 12, 2008 Share Posted October 12, 2008 <?php echo date("d",strtotime("2008-10-09") - strtotime("2008-09-26")); ?> //13 That does not work as you'd expect!! Don't do it this way!!! Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663599 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2008 Share Posted October 13, 2008 The code that xtopolis posted won't work when the difference is more than 31 days. Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663601 Share on other sites More sharing options...
ibechane Posted October 13, 2008 Share Posted October 13, 2008 Let me explain: strtotime() is similar to mktime() except that it allows you to input the date as strtotime(mm-dd-yyyy) instead of mktime(hour,minute,second,month,day,year). They both return timestamps, i.e. the number of seconds since Jan.1, 1970. When you subtract them, you get the number of seconds between the two dates, which is ok. This is useful. HOWEVER, date() does not convert seconds to days. It converts a time stamp to a DATE. In other words, if you gave date() a time stamp of 86401 (the number of seconds in 1 day plus 1 second), it will calculate to Jan 2, 1970. The first argument of date() is the FORMAT of the date. The letter "d" refers to the day portion of the date, which in the case of the time stamp 86401 is "2." If the dates are significantly spread out, or straddles more than one month, xtopolis' method will not work. Please read: date(): http://us.php.net/date time(): http://us.php.net/time mktime(): http://us.php.net/mktime Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663602 Share on other sites More sharing options...
xtopolis Posted October 13, 2008 Share Posted October 13, 2008 apologies for a not providing a fit all solution. Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663603 Share on other sites More sharing options...
davidkierz Posted October 13, 2008 Author Share Posted October 13, 2008 i like the timestamp option, as it seems the most accurate i am going to change my sql date variable to handle the date as a timestamp rather than date, and adjust my code accordingly thanks to all Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-663622 Share on other sites More sharing options...
davidkierz Posted October 13, 2008 Author Share Posted October 13, 2008 sorry to wake up a solved old thread but i got a another question... now that i have my sql database populated with events timestamped with unixtime, is there a way i can use a sql command to select rows based on the day of week? i would assume there would have to be a built in time function..... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/128132-solved-difference-between-2-dates-function/#findComment-664456 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.