Jump to content

Recommended Posts

Hi,

 

I have the following code which lists all the dates since October 5th 2007 until yesterday's date, and displays it as a link. This part works fine. What I want to do is omit the weekends, so the first few lines would read like so:

 

05/10/2007

08/10/2007

09/10/2007

10/10/2007

11/10/2007

12/10/2007

15/10/2007

 

Does anyone know how I can modify this code, or have any other methods of doing this?  :confused:

 

<?php function dates_between($start_date, $end_date = false) 
{ 
     if ( !$end_date ) 
     { 
         $end_date = date("Y-m-d"); 
     } 
      
     $start_date = is_int($start_date) ? $start_date : strtotime($start_date); 
     $end_date = is_int($end_date) ? $end_date : strtotime($end_date); 
      
     $end_date -= (60 * 60 * 24 + 1); 
      
     $test_date = $start_date; 
     $day_incrementer = 1; 
      
     do  
     { 
         $test_date = $start_date + ($day_incrementer * 60 * 60 * 24); 
         echo '<a href="http://markets.ft.com/RESEARCH/markets/DataArchiveFetchReport?Category=CU&Type=WORL&Date=' . date("m/d/Y", $test_date) . '">' . date("d/m/Y", $test_date) . "\n</a><br>"; 
     } while ( $test_date < $end_date && ++$day_incrementer ); 
} 
  
dates_between("2007-10-04"); 
echo "\n\n"; ?>

 

Thanks,

Dave

Link to comment
https://forums.phpfreaks.com/topic/255180-date-function-help/
Share on other sites

You could test for the day number, and only echo it if it's less than 6.

 

do {
	$test_date = $start_date + ($day_incrementer * 60 * 60 * 24);
	if( date('N', $test_date) < 6 ) { // ADDED THIS CONDITIONAL //
		echo '<a href="http://markets.ft.com/RESEARCH/markets/DataArchiveFetchReport?Category=CU&Type=WORL&Date=' . date("m/d/Y", $test_date) . '">' . date("d/m/Y", $test_date) . "\n</a><br>";
	}

Link to comment
https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308384
Share on other sites

You could test for the day number, and only echo it if it's less than 6.

 

do {
	$test_date = $start_date + ($day_incrementer * 60 * 60 * 24);
	if( date('N', $test_date) < 6 ) { // ADDED THIS CONDITIONAL //
		echo '<a href="http://markets.ft.com/RESEARCH/markets/DataArchiveFetchReport?Category=CU&Type=WORL&Date=' . date("m/d/Y", $test_date) . '">' . date("d/m/Y", $test_date) . "\n</a><br>";
	}

 

Thank you very much for that, it's working now :)

 

However, I now have another question - I want to split the list in to months, so that there is a list for January 2012, another list for December 2011 and so on, rather than one indigestible long list. This normally wouldn't be a problem, as I could just repeat the script and hard code the dates for the start and end of each month. However, if I do it for this month and in the future months, I would get the following output:

 

16/01/2012

17/01/2012

18/01/2012

19/01/2012

20/01/2012

 

I don't want any future dates to be shown, rather I would prefer for it to show up until yesterdays date (16/01/2012). Any ideas?

 

Cheers,

Dave

Link to comment
https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308513
Share on other sites

To detect and output a new month heading or just a break in the output, you would need to 'remember' the month (name or number) and detect when the month (name or number) changes. You can get the month (name or number) using date() with one of the month format specifiers.

 

The function you found takes a second parameter to specific the end date. You would just produce a date one less than the current date and supply that as a parameter when you call the function.

Link to comment
https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308557
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.