IT Community - Software Programming, Web Development and Technical Support

How can we know the number of days between two given dates using PHP?

This is a discussion on How can we know the number of days between two given dates using PHP? within the PHP Programming forums, part of the Web Development category; How can we know the number of days between two given dates using PHP?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 09-11-2007, 07:11 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Exclamation How can we know the number of days between two given dates using PHP?

How can we know the number of days between two given dates using PHP?
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 09-21-2007, 02:20 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 246
vijayanand is on a distinguished road
Default Re: How can we know the number of days between two given dates using PHP?

hi sabari,

Using the following function u can get the no of days between two dates.

PHP Code:
function dateDiff($dformat$endDate$beginDate)
{
$date_parts1=explode($dformat$beginDate);
$date_parts2=explode($dformat$endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return 
$end_date $start_date;

Explanation:
Now let us see how we use this function:

$date1="07/11/2003";
$date2="09/04/2004";

print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";


which generates If we minus 07/11/2003 from 09/04/2004 we get 421.
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 09-21-2007, 04:11 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: How can we know the number of days between two given dates using PHP?

This code is not working for me?

Error:

Fatal error: Call to undefined function: gregoriantojd() in /home/www/htdocs/photomax_clone/testing/sabari/scalar_error1.php on line 7
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 09-21-2007, 04:17 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: How can we know the number of days between two given dates using PHP?

I'm using this version of PHP Version 4.3.4?
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 09-27-2007, 04:00 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 157
jegan is on a distinguished road
Default Re: How can we know the number of days between two given dates using PHP?

I think this will work out..


<?
$date1 = '2007/09/27';
$date2 = '2007/09/25';

datediff($date1,$date2);

function datediff($d1,$d2)
{
$days = (strtotime($d1) - strtotime($d2)) / (60 * 60 * 24);
echo "Number of days between $d1 and $d2 : $days";
}
?>
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 09-27-2007, 06:24 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: How can we know the number of days between two given dates using PHP?

hi,
her i have given you the function to get year diffrence. it will help you to get age.

PHP Code:
function getYearDiff($pmDateFrom$pmDateTo$using_timestamps true
{
    
          
/*
            $interval can be:
            yyyy - Number of full years
            q - Number of full quarters
            m - Number of full months
            y - Difference between day numbers
              (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
            d - Number of full days
            w - Number of full weekdays
            ww - Number of full weeks
            h - Number of full hours
            n - Number of full minutes
            s - Number of full seconds (default)
          */
          
          //# - default intervals 
          
$interval='yyyy';
          if (!
$using_timestamps) {
          
            
$pmDateFrom strtotime($pmDateFrom0);
            
$pmDateTo strtotime($pmDateTo0);
         }
          
          
$difference $pmDateTo $pmDateFrom// Difference in seconds
         
switch($interval) {
          
            case 
'yyyy'// Number of full years
        
              
$years_difference floor($difference 31536000);
              if (
mktime(date("H"$pmDateFrom), date("i"$pmDateFrom), date("s"$pmDateFrom), date("n"$pmDateFrom), date("j"$pmDateFrom), date("Y"$pmDateFrom)+$years_difference) > $pmDateTo) {
                
$years_difference--;
              }
              if (
mktime(date("H"$pmDateTo), date("i"$pmDateTo), date("s"$pmDateTo), date("n"$pmDateTo), date("j"$pmDateTo), date("Y"$pmDateTo)-($years_difference+1)) > $pmDateFrom) {
                
$years_difference++;
              }
              
$datediff $years_difference;
              break;
        
            case 
"q"// Number of full quarters
        
              
$quarters_difference floor($difference 8035200);
              while (
mktime(date("H"$pmDateFrom), date("i"$pmDateFrom), date("s"$pmDateFrom), date("n"$pmDateFrom)+($quarters_difference*3), date("j"$pmDateTo), date("Y"$pmDateFrom)) < $pmDateTo) {
                
$months_difference++;
              }
              
$quarters_difference--;
              
$datediff $quarters_difference;
              break;
        
            case 
"m"// Number of full months
        
              
$months_difference floor($difference 2678400);
              while (
mktime(date("H"$pmDateFrom), date("i"$pmDateFrom), date("s"$pmDateFrom), date("n"$pmDateFrom)+($months_difference), date("j"$pmDateTo), date("Y"$pmDateFrom)) < $pmDateTo) {
                
$months_difference++;
              }
              
$months_difference--;
              
$datediff $months_difference;
              break;
        
            case 
'y'// Difference between day numbers
        
              
$datediff date("z"$pmDateTo) - date("z"$pmDateFrom);
              break;
        
            case 
"d"// Number of full days
        
              
$datediff floor($difference 86400);
              break;
        
            case 
"w"// Number of full weekdays
        
              
$days_difference floor($difference 86400);
              
$weeks_difference floor($days_difference 7); // Complete weeks
              
$first_day date("w"$pmDateFrom);
              
$days_remainder floor($days_difference 7);
              
$odd_days $first_day $days_remainder// Do we have a Saturday or Sunday in the remainder?
              
if ($odd_days 7) { // Sunday
                
$days_remainder--;
              }
              if (
$odd_days 6) { // Saturday
                
$days_remainder--;
              }
              
$datediff = ($weeks_difference 5) + $days_remainder;
              break;
        
            case 
"ww"// Number of full weeks
        
              
$datediff floor($difference 604800);
              break;
        
            case 
"h"// Number of full hours
        
              
$datediff floor($difference 3600);
              break;
        
            case 
"n"// Number of full minutes
        
              
$datediff floor($difference 60);
              break;
        
            default: 
// Number of full seconds (default)
        
              
$datediff $difference;
              break;
          }    
        
          return 
$datediff;
        

__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of days in a month (or last day of a month) itbarota HTML, CSS and Javascript Coding Techniques 2 04-28-2008 07:47 PM
Show the number of days, hours and seconds till Y3K. itbarota HTML, CSS and Javascript Coding Techniques 1 03-10-2008 12:28 AM
calculate the number of days in the current month itbarota HTML, CSS and Javascript Coding Techniques 1 02-08-2008 05:30 AM
How can I calculate the number of days elapsed between two dates? itbarota HTML, CSS and Javascript Coding Techniques 2 12-19-2007 08:14 PM
how to calculate number of sundays between two dates in javascript oxygen HTML, CSS and Javascript Coding Techniques 1 07-30-2007 08:49 AM


All times are GMT -7. The time now is 07:03 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0