Fenhopi Posted November 6, 2010 Share Posted November 6, 2010 Hi, I'm looking for a code to view the date of the past weekdays. Forexample, if it's thursday, I want it to view the dates for wednesday, tuesday, monday, Friday before. Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/217923-a-code-to-view-the-date-of-the-past-weekdays/ Share on other sites More sharing options...
Airzooka Posted November 6, 2010 Share Posted November 6, 2010 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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217923-a-code-to-view-the-date-of-the-past-weekdays/#findComment-1131009 Share on other sites More sharing options...
jl5501 Posted November 6, 2010 Share Posted November 6, 2010 Hello You may find this useful echo date('l dS \o\f F Y h:i:s A', strtotime('last thursday')); Where you can add/remove specifiers in the date() function to change how the date is displayed Quote Link to comment https://forums.phpfreaks.com/topic/217923-a-code-to-view-the-date-of-the-past-weekdays/#findComment-1131067 Share on other sites More sharing options...
Andy-H Posted November 6, 2010 Share Posted November 6, 2010 $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 > Quote Link to comment https://forums.phpfreaks.com/topic/217923-a-code-to-view-the-date-of-the-past-weekdays/#findComment-1131073 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.