Randall-B
asked on
Output Previous Weekday from Input Like "030507" (March 5, 2007)
How to output the previous weekday?
If user inputs "030507" (Monday, March 5, 2007), I want to output "030207" (Friday, March 2, 2007), skipping back past Sunday and Saturday.
But if the user inputs "030607" (Tuesday, March 6, 2007), I want to output "030507" (Monday, March 5, 2007).
If user inputs "030507" (Monday, March 5, 2007), I want to output "030207" (Friday, March 2, 2007), skipping back past Sunday and Saturday.
But if the user inputs "030607" (Tuesday, March 6, 2007), I want to output "030507" (Monday, March 5, 2007).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are defining the function 'getPreviousWeekday' within your for() loop, which will cause errors.
Take the entire 'getPreviousWeekday()' function and put it outside your loop:
<?php
$inputDate = "March, 2007";
list($inputMonth, $inputYear) = explode(', ', $inputDate);
$daysInMonth = date('t', strtotime("$inputMonth $inputYear"));
for ($i=1; $i<=$daysInMonth; $i++)
{
if (6 > date('N', strtotime("$i $inputMonth $inputYear")))
{
$weekday = date('mdy', strtotime("$i $inputMonth $inputYear"));
//////////// Get previous weekday /////////
$inputMonth = substr($weekday, 0, 2);
$inputDay = substr($weekday, 2, 2);
$inputYear = substr($weekday, 4, 2);
$timestamp = mktime(0, 0, 0, $inputMonth, $inputDay, $inputYear);
getPreviousWeekday($timest amp);
/////// END of get previous weekday //////////
echo "<tr><td>$weekday $previousweekday</td></tr> ";
}
}
function getPreviousWeekday($timest amp) {
// This function is called recursively until a weekday is found
$previousDay = $timestamp - 86400; // Substract one day of seconds (24 hours x 60 minutes x 60 seconds)
if (6 > date('N', $previousDay)) {
$previousWeekday = date('mdy', $previousDay);
print "$previousWeekday<br />";
} else {
getPreviousWeekday($previo usDay);
}
}
?>
Take the entire 'getPreviousWeekday()' function and put it outside your loop:
<?php
$inputDate = "March, 2007";
list($inputMonth, $inputYear) = explode(', ', $inputDate);
$daysInMonth = date('t', strtotime("$inputMonth $inputYear"));
for ($i=1; $i<=$daysInMonth; $i++)
{
if (6 > date('N', strtotime("$i $inputMonth $inputYear")))
{
$weekday = date('mdy', strtotime("$i $inputMonth $inputYear"));
//////////// Get previous weekday /////////
$inputMonth = substr($weekday, 0, 2);
$inputDay = substr($weekday, 2, 2);
$inputYear = substr($weekday, 4, 2);
$timestamp = mktime(0, 0, 0, $inputMonth, $inputDay, $inputYear);
getPreviousWeekday($timest
/////// END of get previous weekday //////////
echo "<tr><td>$weekday $previousweekday</td></tr>
}
}
function getPreviousWeekday($timest
// This function is called recursively until a weekday is found
$previousDay = $timestamp - 86400; // Substract one day of seconds (24 hours x 60 minutes x 60 seconds)
if (6 > date('N', $previousDay)) {
$previousWeekday = date('mdy', $previousDay);
print "$previousWeekday<br />";
} else {
getPreviousWeekday($previo
}
}
?>
ASKER
OK, that gets rid of that error, but now I get another error.
I want to do:
echo "<tr><td>$weekday $previousWeekday</td></tr> ";
So I removed the echo $previousWeekday line from the function, and am trying to echo $previousWeekay in the table cell along with $weekday. However, only one date is showing, and it is the unix timestamp error.
I want to get this:
030107 022807
030207 030107
030507 030207
. . . etc.
But this is the output:
030107
123169
123169
. . . etc.
I want to do:
echo "<tr><td>$weekday $previousWeekday</td></tr>
So I removed the echo $previousWeekday line from the function, and am trying to echo $previousWeekay in the table cell along with $weekday. However, only one date is showing, and it is the unix timestamp error.
I want to get this:
030107 022807
030207 030107
030507 030207
. . . etc.
But this is the output:
030107
123169
123169
. . . etc.
How about, add the print statement back into the getPreviousWeekday() function, and use this:
echo "<tr><td>$weekday ";
getPreviousWeekday($weekda y);
echo "</td></tr>";
If that does not work, can you post your code as it stands now?
echo "<tr><td>$weekday ";
getPreviousWeekday($weekda
echo "</td></tr>";
If that does not work, can you post your code as it stands now?
ASKER
OK, that made it print two numbers, but not the right ones. It's like this:
030107 123169
123169 010170
123169 010170
123169 010170
. . . etc.
So I'm posting the code as it stood in my previous comment. See: http://216.92.61.99/weekdayphp.txt
But instead of the timestamp-based function, what about using something like this within the original loop, to simply detect if $weekday is a Monday, and if so, subtract 3 days to make it a Friday (but subtract only 1 day from the other weekdays):
if ((date('w'), $weekday) == '0') {
$prevousWeekday = date('mdy', strtotime('- 3 day'));
} else {
$prevousWeekday = date('mdy', strtotime('- 1 day'));
}
I was trying to do that before, but I'm having trouble with the syntax for
if (date('w'), $weekday) == '0')
Should it be:
if (date('w'), strtotime($weekday)) == '0')
? I can't get it to work for some reason.
Thanks.
030107 123169
123169 010170
123169 010170
123169 010170
. . . etc.
So I'm posting the code as it stood in my previous comment. See: http://216.92.61.99/weekdayphp.txt
But instead of the timestamp-based function, what about using something like this within the original loop, to simply detect if $weekday is a Monday, and if so, subtract 3 days to make it a Friday (but subtract only 1 day from the other weekdays):
if ((date('w'), $weekday) == '0') {
$prevousWeekday = date('mdy', strtotime('- 3 day'));
} else {
$prevousWeekday = date('mdy', strtotime('- 1 day'));
}
I was trying to do that before, but I'm having trouble with the syntax for
if (date('w'), $weekday) == '0')
Should it be:
if (date('w'), strtotime($weekday)) == '0')
? I can't get it to work for some reason.
Thanks.
ASKER
I came up with this to convert any date like "030507" to the previous weekday:
<?php
$input = "030507";
echo "$input ";
echo getPreviousWeekday($input) ;
function getPreviousWeekday($input) {
$inputMonth = substr($input, 0, 2);
$inputDay = substr($input, 2, 2);
$inputYear = substr($input, 4, 2);
$timestamp = strtotime("$inputYear-$inp utMonth-$i nputDay");
if (date("w", $timestamp) == 1) { // input is Monday
$previousWeekday = date('mdy', strtotime("-3 day", $timestamp));
} elseif (date("w", $timestamp) == 0) { // input is Sunday
$previousWeekday = date('mdy', strtotime("-2 day", $timestamp));
} else { // input is not Sunday or Monday
$previousWeekday = date('mdy', strtotime("-1 day", $timestamp));
}
return $previousWeekday;
}
?>
and the entire working script, which prints all weekdays (and their previousweekday) in a given month, is:
<html>
<body>
<table>
<?php
$inputDate = "March, 2007";
list($inputMonth, $inputYear) = explode(', ', $inputDate);
$daysInMonth = date('t', strtotime("$inputMonth $inputYear"));
for ($i=1; $i<=$daysInMonth; $i++) {
if (6 > date('N', strtotime("$i $inputMonth $inputYear"))) {
$weekday = date('mdy', strtotime("$i $inputMonth $inputYear"));
$previousweekday = getPreviousWeekday($weekda y);
echo "<tr><td>$weekday $previousweekday</td></tr> ";
}
}
function getPreviousWeekday($input) {
$inputMonth = substr($input, 0, 2);
$inputDay = substr($input, 2, 2);
$inputYear = substr($input, 4, 2);
$timestamp = strtotime("$inputYear-$inp utMonth-$i nputDay");
if (date("w", $timestamp) == 1) { // input is Monday
$previousWeekday = date('mdy', strtotime("-3 day", $timestamp));
} elseif (date("w", $timestamp) == 0) { // input is Sunday
$previousWeekday = date('mdy', strtotime("-2 day", $timestamp));
} else { // input is not Sunday or Monday
$previousWeekday = date('mdy', strtotime("-1 day", $timestamp));
}
return $previousWeekday;
}
?>
</table>
</body>
</html>
That works, although I'm sure it could be improved.
<?php
$input = "030507";
echo "$input ";
echo getPreviousWeekday($input)
function getPreviousWeekday($input)
$inputMonth = substr($input, 0, 2);
$inputDay = substr($input, 2, 2);
$inputYear = substr($input, 4, 2);
$timestamp = strtotime("$inputYear-$inp
if (date("w", $timestamp) == 1) { // input is Monday
$previousWeekday = date('mdy', strtotime("-3 day", $timestamp));
} elseif (date("w", $timestamp) == 0) { // input is Sunday
$previousWeekday = date('mdy', strtotime("-2 day", $timestamp));
} else { // input is not Sunday or Monday
$previousWeekday = date('mdy', strtotime("-1 day", $timestamp));
}
return $previousWeekday;
}
?>
and the entire working script, which prints all weekdays (and their previousweekday) in a given month, is:
<html>
<body>
<table>
<?php
$inputDate = "March, 2007";
list($inputMonth, $inputYear) = explode(', ', $inputDate);
$daysInMonth = date('t', strtotime("$inputMonth $inputYear"));
for ($i=1; $i<=$daysInMonth; $i++) {
if (6 > date('N', strtotime("$i $inputMonth $inputYear"))) {
$weekday = date('mdy', strtotime("$i $inputMonth $inputYear"));
$previousweekday = getPreviousWeekday($weekda
echo "<tr><td>$weekday $previousweekday</td></tr>
}
}
function getPreviousWeekday($input)
$inputMonth = substr($input, 0, 2);
$inputDay = substr($input, 2, 2);
$inputYear = substr($input, 4, 2);
$timestamp = strtotime("$inputYear-$inp
if (date("w", $timestamp) == 1) { // input is Monday
$previousWeekday = date('mdy', strtotime("-3 day", $timestamp));
} elseif (date("w", $timestamp) == 0) { // input is Sunday
$previousWeekday = date('mdy', strtotime("-2 day", $timestamp));
} else { // input is not Sunday or Monday
$previousWeekday = date('mdy', strtotime("-1 day", $timestamp));
}
return $previousWeekday;
}
?>
</table>
</body>
</html>
That works, although I'm sure it could be improved.
ASKER
And to output the list of weekdays and their previous days in reverse order (from the last weekday of the month down to the 1st weekday of the month), it works with this for . . . loop:
for ($i=$daysInMonth; $i>=1; $i--)
Thanks.
for ($i=$daysInMonth; $i>=1; $i--)
Thanks.
ASKER
I'm accepting your first response as the answer, since it did work as requested in the Question (although the function in my comment above seems to work better in the context of the particular script that I'm using). Thanks!
ASKER
In that situation it gives this error: Fatal error: Call to undefined function getpreviousweekday() . Here is the complete script that I'm testing:
<?php
$inputDate = "March, 2007";
list($inputMonth, $inputYear) = explode(', ', $inputDate);
$daysInMonth = date('t', strtotime("$inputMonth $inputYear"));
for ($i=1; $i<=$daysInMonth; $i++)
{
if (6 > date('N', strtotime("$i $inputMonth $inputYear")))
{
$weekday = date('mdy', strtotime("$i $inputMonth $inputYear"));
//////////// Get previous weekday /////////
$inputMonth = substr($weekday, 0, 2);
$inputDay = substr($weekday, 2, 2);
$inputYear = substr($weekday, 4, 2);
$timestamp = mktime(0, 0, 0, $inputMonth, $inputDay, $inputYear);
getPreviousWeekday($timest
function getPreviousWeekday($timest
// This function is called recursively until a weekday is found
$previousDay = $timestamp - 86400; // Substract one day of seconds (24 hours x 60 minutes x 60 seconds)
if (6 > date('N', $previousDay)) {
$previousWeekday = date('mdy', $previousDay);
print "$previousWeekday<br />";
} else {
getPreviousWeekday($previo
}
}
/////// END of get previous weekday //////////
echo "<tr><td>$weekday $previousweekday</td></tr>
}
}
?>