Jump to content

Calculate days from date to date


thaidomizil

Recommended Posts

Hi there,

 

i am using a form with 2 inputs which are equipped with a datepicker: Date 1 & Date 2, is it possible to calculate how many days are there from Date 1 to Date 2 (including the selected ones) ?

 

On my form the dates are in this format: September 08, 2011 (i guess i can change that to numeric only, if that helps) Tamper data shows them getting posted like this: September+14%2C+2011

 

Any help / hints will be appreciated !

 

Link to comment
Share on other sites

Something like this should work:

 

<?php

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

?> 

Link to comment
Share on other sites

Thats how you do it:

 

<?php
  $check = '10/09/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database Just put in between these two parenthesis the date, wherever it may come from.
  $date = strtotime($check); 
  $event_year = date('Y',$date);
  $event_month = date('n',$date);
  $event_day = date('j',$date);
    
  $difference = ((mktime (0,0,0,$event_month,$event_day,$event_year) - time()) / 3600);
  $days_left  = (int) ($difference/24);
  
   print $days_left . ' days left until ' . $event_day . '-' . $event_month.'-'.$event_year; 
  ?> 

Link to comment
Share on other sites

Thats how you do it:

 

some code here

 

That looks more like a countdown ?

 

Premiso,

 

i guess your code does work, could you hint me in the right direction how i could convert my current date format to the format needed for that script ? the jquery code for that datepicker is all hieroglyphs to me, i can't find out how to change the date format.

 

Thank you

Link to comment
Share on other sites

My code calculate the difference between the current date and given date

you can modify it according to your own sweat will...

 

Hello,

 

i appreciate that, i am quite the beginner with php though so i wouldn't know where to start, i take it i would just have to add another "$check" kind as in the script to specify the second date ?

 

If you could point me in the right direction i would be grateful.

 

Thanks for your effort.

Link to comment
Share on other sites

THE WORKING CODE AS PER YOUR REQUIREMENT:

 

<?php
  $check = '10/09/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database
  $check1 = '10/11/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database

  $date = strtotime($check);
   $date1 = strtotime($check1);
  $event_year = date('Y',$date);
  $event_month = date('n',$date);
  $event_day = date('j',$date);
  $event_year1 = date('Y',$date1);
  $event_month1 = date('n',$date1);
  $event_day1 = date('j',$date1);
    
  $difference = ((mktime (0,0,0,$event_month,$event_day,$event_year)) / 3600);
   $difference1 = ((mktime (0,0,0,$event_month1,$event_day1,$event_year1)) / 3600);
  $diff= $difference1 - $difference;
  $days_left  = (int) ($diff/24);
  
   print $days_left . ' days left until ' . $event_day . '-' . $event_month.'-'.$event_year; 
  ?> 

Link to comment
Share on other sites

THE WORKING CODE AS PER YOUR REQUIREMENT:

 

<?php
  $check = '10/09/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database
  $check1 = '10/11/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database

  $date = strtotime($check);
   $date1 = strtotime($check1);
  $event_year = date('Y',$date);
  $event_month = date('n',$date);
  $event_day = date('j',$date);
  $event_year1 = date('Y',$date1);
  $event_month1 = date('n',$date1);
  $event_day1 = date('j',$date1);
    
  $difference = ((mktime (0,0,0,$event_month,$event_day,$event_year)) / 3600);
   $difference1 = ((mktime (0,0,0,$event_month1,$event_day1,$event_year1)) / 3600);
  $diff= $difference1 - $difference;
  $days_left  = (int) ($diff/24);
  
   print $days_left . ' days left until ' . $event_day . '-' . $event_month.'-'.$event_year; 
  ?> 

 

Works like a charm, where's your donate button ? :)

 

Thanks a bunch !

Link to comment
Share on other sites

THE WORKING CODE AS PER YOUR REQUIREMENT:

 

<?php
  $check = '10/09/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database
  $check1 = '10/11/11'; // Set it to the recordset or a variable from where the date is comming from the specific field from database

  $date = strtotime($check);
   $date1 = strtotime($check1);
  $event_year = date('Y',$date);
  $event_month = date('n',$date);
  $event_day = date('j',$date);
  $event_year1 = date('Y',$date1);
  $event_month1 = date('n',$date1);
  $event_day1 = date('j',$date1);
    
  $difference = ((mktime (0,0,0,$event_month,$event_day,$event_year)) / 3600);
   $difference1 = ((mktime (0,0,0,$event_month1,$event_day1,$event_year1)) / 3600);
  $diff= $difference1 - $difference;
  $days_left  = (int) ($diff/24);
  
   print $days_left . ' days left until ' . $event_day . '-' . $event_month.'-'.$event_year; 
  ?> 

 

Works like a charm, where's your donate button ? :)

 

Thanks a bunch !

 

I am glad your issue is being resolved I learnt it as well lolx! what do you mean by donate button??

Link to comment
Share on other sites

Excuse me, but that code is completely unnecessary and illogical. It makes absolutely no sense to use strtotime() to convert the string date into a timestamp THEN to use that timestamp to get the parts for the dates only to convert those parts BACK into a timestamp!

 

Plus, that does not count the days inclusive of the selected dates (need to add 1). All you need is a single line function:

function date_diff($firstDate, $lastDate)
{
    return (int) ((strtotime($lastDate)-strtotime($firstDate))/86400)+1;
}

$startDate = 'September 08, 2011';
$endDate   = 'October 13, 2011';
echo "There are " . date_diff($startDate, $endDate) . " day(s) between {$startDate} and {$endDate}";
//Output: There are 36 day(s) between September 08, 2011 and October 13, 2011 

Link to comment
Share on other sites

Excuse me, but that code is completely unnecessary and illogical. It makes absolutely no sense to use strtotime() to convert the string date into a timestamp THEN to use that timestamp to get the parts for the dates only to convert those parts BACK into a timestamp!

 

Plus, that does not count the days inclusive of the selected dates (need to add 1). All you need is a single line function:

function date_diff($firstDate, $lastDate)
{
    return (int) ((strtotime($lastDate)-strtotime($firstDate))/86400)+1;
}

$startDate = 'September 08, 2011';
$endDate   = 'October 13, 2011';
echo "There are " . date_diff($startDate, $endDate) . " day(s) between {$startDate} and {$endDate}";
//Output: There are 36 day(s) between September 08, 2011 and October 13, 2011 

 

YOUR code is simple but its not working if it worked then its better than mine :)

Link to comment
Share on other sites

YOUR code is simple but its not working if it worked then its better than mine :)

 

It does work and it is better. Rather than making a worthless comment such as "its not working" why don't you state how it is not working. It works perfectly fine for me. Maybe you need me to show you how to implement it? Here is some unit testing of that code and the output. Please state which of those results is not correct or provide valid inputs that would result in an incorrect output.

 

Unit tests

function date_diff($firstDate, $lastDate)
{
    return (int) ((strtotime($lastDate)-strtotime($firstDate))/86400)+1;
}

$dates = array(
    array('February 5, 2011', 'April 15, 2011'),
    array('March 15, 2011', 'May 19, 2011'),
    array('January 5, 2011', 'June 12, 2011'),
    array('August 20, 2011', 'August 25, 2011'),
    array('July 14, 2011', 'December 23, 2011'),
    array('December 15, 2011', 'January 20, 2012')
);

foreach($dates as $datePair)
{
    $startDate = $datePair[0];
    $endDate   = $datePair[1];
    echo "There are " . date_diff($startDate, $endDate) . " day(s) between {$startDate} and {$endDate}<br>\n";
}

 

Output

There are 69 day(s) between February 5, 2011 and April 15, 2011
There are 66 day(s) between March 15, 2011 and May 19, 2011
There are 158 day(s) between January 5, 2011 and June 12, 2011
There are 6 day(s) between August 20, 2011 and August 25, 2011
There are 163 day(s) between July 14, 2011 and December 23, 2011
There are 37 day(s) between December 15, 2011 and January 20, 2012

 

And, I notice you didn't address the statement I made about totally unnecessary logic to converting the timestamps into date-parts only to reconstruct them back into timestamps. That was half of the code from the solution you provided and all it would do is waste cpu cycles.

Link to comment
Share on other sites

mjdamato,

 

thank you for the code, which i would want to use as it can use the month names for the dates.

However i'm getting some errors here.

 

Fatal error: Cannot redeclare date_diff() in /Applications/XAMPP/xamppfiles/htdocs/test/flug-und-hotel.php on line 92

 

i just copy pasted the code "as is" without changing anything about the dates yet.

 

Would you please enlighten me how to implement it properly ? :)

 

Thank you

Link to comment
Share on other sites

Fatal error: Cannot redeclare date_diff() in /Applications/XAMPP/xamppfiles/htdocs/test/flug-und-hotel.php on line 92

 

That error means you are trying to create a function that already exists. It looks like in ver 5.3 of PHP there is an actual function called date_diff(). Doh! If you have version 5.3 you can just use that function. If not, rename the function I provided to something else.

 

EDIT: The fact that you got that error would mean that you are using a version of PHP that has that built in function, or possibly you put the code in a loop on your page. Even if you do have a version of PHP with that built-in function you need to make sure the server(s) you will be deploying to will support it as well.

Link to comment
Share on other sites

Fatal error: Cannot redeclare date_diff() in /Applications/XAMPP/xamppfiles/htdocs/test/flug-und-hotel.php on line 92

 

That error means you are trying to create a function that already exists. It looks like in ver 5.3 of PHP there is an actual function called date_diff(). Doh! If you have version 5.3 you can just use that function. If not, rename the function I provided to something else.

 

EDIT: The fact that you got that error would mean that you are using a version of PHP that has that built in function, or possibly you put the code in a loop on your page. Even if you do have a version of PHP with that built-in function you need to make sure the server(s) you will be deploying to will support it as well.

 

How about your 2nd example, would that work out of the box, like so:

<?php
function date_diff($firstDate, $lastDate)
{
    return (int) ((strtotime($lastDate)-strtotime($firstDate))/86400)+1;
}

$dates = array(
    array('February 5, 2011', 'April 15, 2011'),
    array('March 15, 2011', 'May 19, 2011'),
    array('January 5, 2011', 'June 12, 2011'),
    array('August 20, 2011', 'August 25, 2011'),
    array('July 14, 2011', 'December 23, 2011'),
    array('December 15, 2011', 'January 20, 2012')
);

foreach($dates as $datePair)
{
    $startDate = $datePair[0];
    $endDate   = $datePair[1];
    echo "There are " . date_diff($startDate, $endDate) . " day(s) between {$startDate} and {$endDate}<br>\n";
}
?>

 

or not? I'm getting the same error there, can't i just put a break at the end of your first code to show me the result without looping endlessly ? There isn't any script on my side at all, all i want to do is get the result from date 1 to date 2 really :-)

 

/Edit

 

Sorry i didn't see your edit on your post, will phpinfo show me that somewhere ?

 

/Edit 2

 

i renamed the function, it is working fine now !

 

Thank you !

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.