Solved

Counting Days So Far This Year..

Posted on 2008-10-01
3
8,108 Views
Last Modified: 2008-10-24
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

0
Comment
Question by:cstormer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:cstormer
ID: 22621241
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)...
0
 
LVL 3

Accepted Solution

by:
sistemu earned 500 total points
ID: 22622043
Hi,
Here is a much shorter version.
<?php
$var1 = strtotime(date ("Y-m-d H:i:s"));
$var2 = strtotime("01/01/".date('Y') );
$rez = round(($var1 - $var2) / (60*60*24)); //seconds in a day
 
echo 'Today: '.$var1.'<br>';
echo 'Rez: '.$rez;
?>

Open in new window

0
 
LVL 3

Assisted Solution

by:sistemu
sistemu earned 500 total points
ID: 22622121
I'm sorry... that wasn't a final version, I skipped a few things.
Hope you like it.
<?php
function return_days_passed($new_date = 0){
    if($new_date != 0)
        $new_date = strtotime($new_date);
    else
        $new_date = strtotime(date ("Y-m-d H:i:s"));
    $first_day = strtotime("01/01/".date('Y H:i:s',$new_date) );
    return round(($new_date - $first_day) / (60*60*24)); //seconds in a day
}
echo return_days_passed().'<br>';
echo return_days_passed('05/01/2005 8:36pm');
?>

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question