Jump to content

calculating difference between two dates


ignace

Recommended Posts

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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P 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

Link to comment
Share on other sites

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.

Link to comment
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.