ignace Posted November 3, 2007 Share Posted November 3, 2007 if i have a date 15/5/2003 and 30/11/2008 how can i then calculate the number of days between them? my first thought went to using an epoche date (i used 1 jan 1970 00:00:00 which is on a monday) and then calculate from 1 jan 1970 to 15/5/2003 and do the same for 30/11/2008 so i just have to extract them from eachother, however apparently i don't have the programming skills to solve the problem, all help is appreciated! greetz, ignace Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/ Share on other sites More sharing options...
ignace Posted November 3, 2007 Author Share Posted November 3, 2007 i mean i then subtract one from the other Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384196 Share on other sites More sharing options...
Barand Posted November 3, 2007 Share Posted November 3, 2007 You use strtotime() to convert to unix timestamps. Unfortunately it won't work with d/m/y format. try <?php function dmyTime($dt) { list ($d, $m, $y) = explode ('/', $dt); return strtotime("$y-$m-$d"); } $date1 = '15/5/2003'; $date2 = '30/11/2008'; $days = floor((dmyTime($date2) - dmyTime($date1)) / 86400); echo $days; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384221 Share on other sites More sharing options...
Orio Posted November 3, 2007 Share Posted November 3, 2007 Something like this should do the job <?php $date1 = "15/5/2003"; $date2 = "30/11/2008"; $d1 = explode("/",$date1); $d2 = explode("/",$date2); $days = floor(abs(mktime(0,0,0,$d1[1],$d1[0],$d1[2]) - mktime(0,0,0,$d2[1],$d2[0],$d2[2])) / (60*60*24)); echo $days; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384224 Share on other sites More sharing options...
ToonMariner Posted November 3, 2007 Share Posted November 3, 2007 mysql has better support for date manipulation have a look at the datesub/dateadd function in the mysql docs... Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384334 Share on other sites More sharing options...
ignace Posted November 3, 2007 Author Share Posted November 3, 2007 yeah i already thought i would get answers like that, but what i wanna do is program my own mktime() function or datesub(), dateadd() functions, i already looked for the algorithm to perform the calculation however nothing came up even scholar.google.com came up blank, however for what i have read so far you need an epoche date to start from, if possible i want to change the date from 1/1/1970 to the gregorian date and start from their, and then convert it to julian dates. I know this is all been done before and functions have been made however i want to write my own, well not entirely my own but i hate using functions of which i know that i am not able to program them myself, therefore the whole "re-write" once again all help is greetly appreciated, greetz, ignace Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384389 Share on other sites More sharing options...
AndyB Posted November 3, 2007 Share Posted November 3, 2007 yeah i already thought i would get answers like that, but what i wanna do is ... Then I'd suggest an apology to all the people who posted solutions for you and now find the time they invested in helping you was completely wasted. Since you want to write your own functions, write away. If your function doesn't work, feel free to post again with your code and explaining what problem you really have. Quote Link to comment https://forums.phpfreaks.com/topic/75901-calculating-difference-between-two-dates/#findComment-384400 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.