Link to home
Start Free TrialLog in
Avatar of Chris Stormer
Chris Stormer

asked on

Counting Days So Far This Year..

I want to count how many days have passed this year so far.

I've attached how I'm doing right now.. it seems a bit.. robust.

Is there a better way to do this?  Is there a way to calculate business days.
<?php
if( function_exists( 'date_default_timezone_set' ) )
{
	// Set the default timezone to US/Eastern
	date_default_timezone_set( 'US/Eastern' );
}
 
// Will return the number of days between the two dates passed in
function count_days( $a, $b )
{
    // First we need to break these dates into their constituent parts:
    $gd_a = getdate( $a );
    $gd_b = getdate( $b );
 
    // Now recreate these timestamps, based upon noon on each day
    // The specific time doesn't matter but it must be the same each day
    $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
    $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
 
    // Subtract these two numbers and divide by the number of seconds in a
    //  day. Round the result since crossing over a daylight savings time
    //  barrier will cause this time to be off by an hour or two.
    return round( abs( $a_new - $b_new ) / 86400 );
}
 
// Prepare a few dates
$date1 = strtotime( '1/01/2008 12:01am' );
$date2 = strtotime( '10/01/2008 8:36pm' );
$dayspassed = count_days($date1, $date2); 
echo $dayspassed;
?>

Open in new window

Avatar of Chris Stormer
Chris Stormer

ASKER

With the above solution I also ahve issues with it not automatically knowing today's date...

$date1 = strtotime( '1/01/2008 12:01am' );
$date2 = strtotime( '10/01/2008 8:36pm' );

Any help here would be great... (and if there is a way for it to always know what the 1st of the year is)...
ASKER CERTIFIED SOLUTION
Avatar of sistemu
sistemu
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial