• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

PHP List all dates from 6th april to the 5th april the next year, with links per date

in PHP I would like to list all the dates in a year from 6th april to 5th april the next year. i would like to make each one a link so if say 7th of april 2007 did not have any info associated with it in a database (MYSQL) it would show up with (fill in) as a link next to the date or a edit link. ie

6th april 2007 edit
7th april 2007 fill in
8th april 2007 fill in
9th april 2007 edit

plus i have a peice of code that does bank holidays see below is there a way to remove those days and weekend from the list either not listed or listed in red and bold.

<?php

/*
 *    Function to calculate which days are British bank holidays (England & Wales) for a given year.
 *
 *    Created by David Scourfield, 07 August 2006, and released into the public domain.
 *    Anybody may use and/or modify this code.
 *
 *    USAGE:
 *
 *    array calculateBankHolidays(int $yr)
 *
 *    ARGUMENTS
 *
 *    $yr = 4 digit numeric representation of the year (eg 1997).
 *
 *    RETURN VALUE
 *
 *    Returns an array of strings where each string is a date of a bank holiday in the format "yyyy-mm-dd".
 *
 *    See example below
 *
 */

function calculateBankHolidays($yr) {

    $bankHols = Array();

    // New year's:
    switch ( date("w", strtotime("$yr-01-01 12:00:00")) ) {
        case 6:
            $bankHols[] = "$yr-01-03";
            break;
        case 0:
            $bankHols[] = "$yr-01-02";
            break;
        default:
            $bankHols[] = "$yr-01-01";
    }

    // Good friday:
    $bankHols[] = date("Y-m-d", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("$yr-03-21 12:00:00") ));

    // Easter Monday:
    $bankHols[] = date("Y-m-d", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("$yr-03-21 12:00:00") ));

    // May Day:
    if ($yr == 1995) {
        $bankHols[] = "1995-05-08"; // VE day 50th anniversary year exception
    } else {
        switch (date("w", strtotime("$yr-05-01 12:00:00"))) {
            case 0:
                $bankHols[] = "$yr-05-02";
                break;
            case 1:
                $bankHols[] = "$yr-05-01";
                break;
            case 2:
                $bankHols[] = "$yr-05-07";
                break;
            case 3:
                $bankHols[] = "$yr-05-06";
                break;
            case 4:
                $bankHols[] = "$yr-05-05";
                break;
            case 5:
                $bankHols[] = "$yr-05-04";
                break;
            case 6:
                $bankHols[] = "$yr-05-03";
                break;
        }
    }

    // Whitsun:
    if ($yr == 2002) { // exception year
        $bankHols[] = "2002-06-03";
        $bankHols[] = "2002-06-04";
    } else {
        switch (date("w", strtotime("$yr-05-31 12:00:00"))) {
            case 0:
                $bankHols[] = "$yr-05-25";
                break;
            case 1:
                $bankHols[] = "$yr-05-31";
                break;
            case 2:
                $bankHols[] = "$yr-05-30";
                break;
            case 3:
                $bankHols[] = "$yr-05-29";
                break;
            case 4:
                $bankHols[] = "$yr-05-28";
                break;
            case 5:
                $bankHols[] = "$yr-05-27";
                break;
            case 6:
                $bankHols[] = "$yr-05-26";
                break;
        }
    }

    // Summer Bank Holiday:
    switch (date("w", strtotime("$yr-08-31 12:00:00"))) {
        case 0:
            $bankHols[] = "$yr-08-25";
            break;
        case 1:
            $bankHols[] = "$yr-08-31";
            break;
        case 2:
            $bankHols[] = "$yr-08-30";
            break;
        case 3:
            $bankHols[] = "$yr-08-29";
            break;
        case 4:
            $bankHols[] = "$yr-08-28";
            break;
        case 5:
            $bankHols[] = "$yr-08-27";
            break;
        case 6:
            $bankHols[] = "$yr-08-26";
            break;
    }

    // Christmas:
    switch ( date("w", strtotime("$yr-12-25 12:00:00")) ) {
        case 5:
            $bankHols[] = "$yr-12-25";
            $bankHols[] = "$yr-12-28";
            break;
        case 6:
            $bankHols[] = "$yr-12-27";
            $bankHols[] = "$yr-12-28";
            break;
        case 0:
            $bankHols[] = "$yr-12-26";
            $bankHols[] = "$yr-12-27";
            break;
        default:
            $bankHols[] = "$yr-12-25";
            $bankHols[] = "$yr-12-26";
    }

    // Millenium eve
    if ($yr == 1999) {
        $bankHols[] = "1999-12-31";
    }

    return $bankHols;

}

/*
 *    EXAMPLE:
 *
 */



$bankHolsThisYear = calculateBankHolidays(2007);

print_r($bankHolsThisYear);
0
martin69
Asked:
martin69
  • 6
  • 5
1 Solution
 
BinkersCommented:
I am not sure what logic you are using to tell if you need an edit or fill in link but the following code should list all the dates you need.

$year = 2007;

$bankHolsThisYear = calculateBankHolidays($year);
$bankHolsNextYear = calculateBankHolidays($year+1);

$done = false;

$print_date = "$year-04-06";
while (!$done) {
      $dateTimeArr = getdate(strtotime($print_date));
      if (!in_array($print_date, $bankHolsThisYear) && $dateTimeArr[weekday] != 'Saturday' && $dateTimeArr[weekday] != 'Sunday') {
            echo "$print_date";
            echo " <a href=\"link\">edit</a>";
            echo "<br/>\n";
      }
      $print_date = date('Y-m-d', mktime($dateTimeArr[hours], $dateTimeArr[minutes], $dateTimeArr[seconds], $dateTimeArr[mon], $dateTimeArr[mday]+1, $dateTimeArr[year]));
      if ($print_date == ($year+1) . "-04-05") {
            $done = true;
      }
}
echo "$print_date";
echo " <a href=\"link\">edit</a>";
echo "<br/>\n";
0
 
martin69Author Commented:
wow thats brilliant. it misses a few days the 6th was a friday and the 9th was a monday but it starts at 10th on your code. but cant see where it goes wrong. also is it possible to make the saturdays sundays come up in red with no link next to them.

the way i was going to check the link if it is edit or fill in would be by mysql

so like select from timesheetcab where date = $date1 and session = session1

and last one can it be made as well to list it in weekly blocks with next and prev buttons?

thanks alot so far brilliant
0
 
BinkersCommented:
The 6th and 9th show up as bank holidays so they are removed from the list because of that.  If you wanted them to show up differently instead of hiding them you could do something like.

 if (!in_array($print_date, $bankHolsThisYear) && $dateTimeArr[weekday] != 'Saturday' && $dateTimeArr[weekday] != 'Sunday') {
            echo "$print_date";
            echo " <a href=\"link\">edit</a>";
            echo "<br/>\n";
} else {
           echo "<span style=\"color:#FF0000; font-weight:bold\">$print_date</span><br/>\n";
}
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
martin69Author Commented:
brilliant. im a pain i know. can i get it to show bank holidays in say green.

then just an if state ment for where i would put my mysql code for the link or fillin.

then if i can get it to show only a week at a time. and show week by current date but can go to previous months.

thank you so much for your help really appricate it.

sorry for been a pain

martin
0
 
BinkersCommented:
To have bank holidays show up in green and weekend in red you could do this:

if (in_array($print_date, $bankHolsThisYear) || in_array($print_date, $bankHolsNextYear) {
   echo "<span style=\"color:#009900; font-weight:bold\">$print_date</span><br/>\n";
} else if ($dateTimeArr[weekday] == 'Saturday' || $dateTimeArr[weekday] == 'Sunday') {
   echo "<span style=\"color:#FF0000; font-weight:bold\">$print_date</span><br/>\n";
} else {
   echo "$print_date";
   if (your_mysql_function()) {
      echo " <a href=\"link1\">edit</a>";
   } else {
      echo " <a href=\"link2\">fillin</a>";
   }
   echo "<br/>\n";
}
0
 
martin69Author Commented:
it keeps tell me:-

Parse error: syntax error, unexpected '{' in /home/archsol/public_html/cit/test.php on line 211

have i pasted it in the wrong place?
0
 
BinkersCommented:
I missed the end to the parentheses in the first if statement so if you add one between ) and { you should be  fine.
0
 
martin69Author Commented:
right got that going. just my mysql bit makes it do to edits with no dates then the date after that. see code below

so it looks like
2007-04-10 edit
edit
edit
2007-04-11 edit

but i also missed something as the 2007-04-10 edit would say 2007-04-11 Fill in as it is in the mysql table.

code:
$year = 2007;

$bankHolsThisYear = calculateBankHolidays($year);
$bankHolsNextYear = calculateBankHolidays($year+1);

$done = false;

$print_date = "$year-04-06";
while (!$done) {
      $dateTimeArr = getdate(strtotime($print_date));
      if (in_array($print_date, $bankHolsThisYear) || in_array($print_date, $bankHolsNextYear)) {
   echo "<span style=\"color:#009900; font-weight:bold\">$print_date</span><br/>\n";
} else if ($dateTimeArr[weekday] == 'Saturday' || $dateTimeArr[weekday] == 'Sunday') {
   echo "<span style=\"color:#FF0000; font-weight:bold\">$print_date</span><br/>\n";
} else {
   echo "$print_date";
   include ("/home/archsol/mysql.inc");
            $query = "SELECT * FROM timesheetcab";
$result = mysql_query($query) or die("Error: " . mysql_error());
$count = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
   if ($row['tsdate']=$print_date) {
      echo " <a href=\"link1\">edit</a>";
   } else {
      echo " <a href=\"link2\">fillin</a>";
   }
   echo "<br/>\n";
}
}
      $print_date = date('Y-m-d', mktime($dateTimeArr[hours], $dateTimeArr[minutes], $dateTimeArr[seconds], $dateTimeArr[mon], $dateTimeArr[mday]+1, $dateTimeArr[year]));
      if ($print_date == ($year+1) . "-04-05") {
            $done = true;
      }
}
echo "$print_date";
echo " <a href=\"link\">edit</a>";
echo "<br/>\n";

thanks for your help really am greatful. after the mysql part if there a way or listing the dates in a weekly section with next and prev at all.
0
 
martin69Author Commented:
right im getting there. see code below. it goes not seem to use the else print fill in link. it iwll show up all the edits next to all the dates i put in the date base and it looks right now.

plus will need to put if statement to the end of the list as 2008-04-05 always shows edit.

hopefully i've just missed something why its not using the else. then just wondering is there is a way to make the dates into smaller chunks.

<?
$year = 2007;

$bankHolsThisYear = calculateBankHolidays($year);
$bankHolsNextYear = calculateBankHolidays($year+1);

$done = false;

$print_date = "$year-04-06";
while (!$done) {
      $dateTimeArr = getdate(strtotime($print_date));
      if (in_array($print_date, $bankHolsThisYear) || in_array($print_date, $bankHolsNextYear)) {
   echo "<span style=\"color:#009900; font-weight:bold\">$print_date</span><br/>\n";
} else if ($dateTimeArr[weekday] == 'Saturday' || $dateTimeArr[weekday] == 'Sunday') {
   echo "<span style=\"color:#FF0000; font-weight:bold\">$print_date</span><br/>\n";
} else {
 
echo "$print_date";
   
   include ("/home/archsol/mysql.inc");
            $query = "SELECT tsdate FROM timesheetcab WHERE tsdate = '$print_date'";
$result = mysql_query($query) or die("Error: " . mysql_error());
while(list($tsdate) = mysql_fetch_row($result))
     
   if ($tsdate=$print_date) {
      echo " <a href=\"link1\">edit</a>";
   } else {
      echo " <a href=\"link2\">fillin</a>";
   }
     echo "<br/>\n";
}
      $print_date = date('Y-m-d', mktime($dateTimeArr[hours], $dateTimeArr[minutes], $dateTimeArr[seconds], $dateTimeArr[mon], $dateTimeArr[mday]+1, $dateTimeArr[year]));
     
        if ($print_date == ($year+1) . "-04-05") {
            $done = true;
      }
}
echo "$print_date";
echo " <a href=\"link\">edit</a>";
echo "<br/>\n";
?>
0
 
BinkersCommented:
In your if statement I believe that you need a double equals like this.

 if ($tsdate=$print_date)

The single equals assigns the value of $print_date to $tsdate.

To break this up into a smaller time frame you would need to modify the setting of the initial print date to be whatever you want your start date to be.  To change the end date you will want to change the following statement to be whatever your end date is.

if ($print_date == ($year+1) . "-04-05") {
      $done = true;
}
could be changed to

if ($print_date == $end_date) {
      $done = true;
}
where $end_date is set earlier in the code.
0
 
martin69Author Commented:
thanks for all your help i have a few more questions i'll add them as new so you can get the points.

thanks alot
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now