Jump to content

A code to view the date of the past weekdays


Fenhopi

Recommended Posts

Here's a little something-something.

 

If you want to retrieve more weekdays, change the number in the "$i < 6" part of the "for" loop to a higher number.

 

If you want to change the format of the dates that are printed (IE, "11-06-2010" instead of "November 6, 2010"), check out this, then edit the date() function.

 

<?php

// Prepare integer representation of days of the week
$dayNames = array(0 => 'Sunday',
                  1 => 'Monday',
                  2 => 'Tuesday',
                  3 => 'Wednesday',
                  4 => 'Thursday',
                  5 => 'Friday',
                  6 => 'Saturday');

// Get the current day
$currentDay = date('w', time());

// Find the last 5 weekdays
$counter = 1;
$day = $currentDay - $counter;
if ($day == -1) {
  $day = 6;
}
for ($i = 1; $i < 6; $i++) {
  $day = $currentDay - $counter;
  while ($day == 0 || $day == 6) {
    $counter++;
    $day = $currentDay - $counter;
    if ($day == -1) {
      $day = 6;
    }
  }
  echo date('F j, Y', strtotime('Last ' . $dayNames[$day])) . '<br />';
  $counter++;
}

?>

$days = array(
               'Mon' => 1,
               'Tue' => 2,
               'Wed' => 3,
               'Thu' => 4,
               'Fri' => 5,
               'Sat' => 6,
               'Sun' => 7
             );
             
$today = date('D');
for($i= $days[$today]; $i > 0; $i--)
{
   if ($i == 6 || $i == 7)
      continue;
   echo date('D, dS M Y', strtotime('-' . $i . ' days')) . '<br >' . "\r\n";
}

 

Output:

Mon, 01st Nov 2010<br >
Tue, 02nd Nov 2010<br >
Wed, 03rd Nov 2010<br >
Thu, 04th Nov 2010<br >
Fri, 05th Nov 2010<br >

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.