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++;
}

?>

Link to comment
Share on other sites

$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 >

 

Link to comment
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.