Icebergness Posted January 16, 2012 Share Posted January 16, 2012 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? <?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 Quote Link to comment https://forums.phpfreaks.com/topic/255180-date-function-help/ Share on other sites More sharing options...
litebearer Posted January 16, 2012 Share Posted January 16, 2012 maybe add a simple - if day date = Sat or day date = Sun continue Quote Link to comment https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308378 Share on other sites More sharing options...
Pikachu2000 Posted January 16, 2012 Share Posted January 16, 2012 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308384 Share on other sites More sharing options...
Icebergness Posted January 17, 2012 Author Share Posted January 17, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308513 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2012 Share Posted January 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255180-date-function-help/#findComment-1308557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.