Get # day names from DateTime and into the past


//*************************************************
//*************************************************
//********** GET X DAY NAMES IN THE PAST **********
//*************************************************
//*************************************************
//$DayChangeHour
//  Normally 0.  Set to 2 is say you want your day to change at 2am instead of midnight (i.e. you'd stil view 01:59 as the evening of the day before)
//$DayNameChar
//  'D' = Mon through Sun, 'l' = 	Sunday through Saturday
//Returns an array:
//  [0] Todays day name
//  [1] Previous day name
//  [#] etc
/* Example usage
  $DayNames = GetXDayNamesInThePast($ServerTimeNow, 7, "D", 4);
  print_r($DayNames);
*/
function GetXDayNamesInThePast ($DateTimeNow, $NumberOfPastDayNames, $DayNameChar, $DayChangeHour)
{
  $Output = array();
  
  
  $DateTimeNow = strtotime($DateTimeNow);

  //----- ARE WE INTO A NEW DAY? -----
  if ( intval((date('G', $DateTimeNow))) >= $DayChangeHour )
  {
    //WE ARE INTO THE NEW DAY
    
  }
  else
  {
    //WE ARE STILL PART OF YESTERDAY (e.g. its 1am)
    $DateTimeNow = strtotime('-1 day',$DateTimeNow);
  }
  
  //----- FILL THE ARRAY WITH THE DAY NAMES -----
  $Output[] = date($DayNameChar, $DateTimeNow);
  while ($NumberOfPastDayNames--)
  {
    $DateTimeNow = strtotime('-1 day',$DateTimeNow);
    $Output[] = date($DayNameChar, $DateTimeNow);
  }
  
  return($Output);
}

Get # day names from DateTime and into the future


//***************************************************
//***************************************************
//********** GET X DAY NAMES IN THE FUTURE **********
//***************************************************
//***************************************************
//$DayChangeHour
//  Normally 0.  Set to 2 is say you want your day to change at 2am instead of midnight (i.e. you'd stil view 01:59 as the evening of the day before)
//$DayNameChar
//  'D' = Mon through Sun, 'l' = 	Sunday through Saturday
//Returns an array:
//  [0] Todays day name
//  [1] Next day name
//  [#] etc
/* Example usage
  $DayNames = GetXDayNamesInThePast($ServerTimeNow, 7, "D", 4);
  print_r($DayNames);
*/
function GetXDayNamesInTheFuture ($DateTimeNow, $NumberOfFutureDayNames, $DayNameChar, $DayChangeHour)
{
  $Output = array();
  
  
  $DateTimeNow = strtotime($DateTimeNow);

  //----- ARE WE INTO A NEW DAY? -----
  if ( intval((date('G', $DateTimeNow))) >= $DayChangeHour )
  {
    //WE ARE INTO THE NEW DAY
    
  }
  else
  {
    //WE ARE STILL PART OF YESTERDAY (e.g. its 1am)
    $DateTimeNow = strtotime('-1 day',$DateTimeNow);
  }
  
  //----- FILL THE ARRAY WITH THE DAY NAMES -----
  $Output[] = date($DayNameChar, $DateTimeNow);
  while ($NumberOfPastDayNames--)
  {
    $DateTimeNow = strtotime('+1 day',$DateTimeNow);
    $Output[] = date($DayNameChar, $DateTimeNow);
  }
  
  return($Output);
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.