Page 1 of 1

Date app not behaving as expected $_POST mktime 5.1.6

Posted: Fri Aug 15, 2008 4:44 am
by rockatansky
All,
Using code found elsewhere on this forum viewtopic.php?f=1&t=86615 I have written a date app that returns the first and last dates of a given day, month and year. So if I enter '2009', 'January', 'Thursday' the app returns '2009-01-01' and '2009-01-29'. The code also resets some form elements and displays some variables for debugging purposes.

Code: Select all

<html>
<body>
 
<!-- Form elements with selected reset logic -->
 
<form action="date.php" method="post">
<select name="year">
<option value="2008"<?php if ($_POST["year"] == 2008) {echo " selected";} ?>>2008</option>
<option value="2009"<?php if ($_POST["year"] == 2009) {echo " selected";} ?>>2009</option>
<option value="2010"<?php if ($_POST["year"] == 2010) {echo " selected";} ?>>2010</option>
</select>
<select name="month">
<option value="01"<?php if ($_POST["month"] == 01) {echo " selected";} ?>>January</option>
<option value="02"<?php if ($_POST["month"] == 02) {echo " selected";} ?>>February</option>
<option value="03"<?php if ($_POST["month"] == 03) {echo " selected";} ?>>March</option>
<option value="04"<?php if ($_POST["month"] == 04) {echo " selected";} ?>>April</option>
<option value="05"<?php if ($_POST["month"] == 05) {echo " selected";} ?>>May</option>
<option value="06"<?php if ($_POST["month"] == 06) {echo " selected";} ?>>June</option>
<option value="07"<?php if ($_POST["month"] == 07) {echo " selected";} ?>>July</option>
<option value="08"<?php if ($_POST["month"] == 08) {echo " selected";} ?>>August</option>
<option value="09"<?php if ($_POST["month"] == 09) {echo " selected";} ?>>September</option>
<option value="10"<?php if ($_POST["month"] == 10) {echo " selected";} ?>>October</option>
<option value="11"<?php if ($_POST["month"] == 11) {echo " selected";} ?>>November</option>
<option value="12"<?php if ($_POST["month"] == 12) {echo " selected";} ?>>December</option>
</select>
<select name="day">
<option value="01"<?php if ($_POST["day"] == 01) {echo " selected";} ?>>Monday</option>
<option value="02"<?php if ($_POST["day"] == 02) {echo " selected";} ?>>Tuesday</option>
<option value="03"<?php if ($_POST["day"] == 03) {echo " selected";} ?>>Wednesday</option>
<option value="04"<?php if ($_POST["day"] == 04) {echo " selected";} ?>>Thursday</option>
<option value="05"<?php if ($_POST["day"] == 05) {echo " selected";} ?>>Friday</option>
<option value="06"<?php if ($_POST["day"] == 06) {echo " selected";} ?>>Saturday</option>
<option value="07"<?php if ($_POST["day"] == 07) {echo " selected";} ?>>Sunday</option>
</select>
<input type="submit">
</form>
<BR>
<BR>
</body>
</html>
 
<?php
/* Variable return for peace of mind */
echo $_POST["day"] . "<p>";
echo $_POST["month"] . "<p>";
echo $_POST["year"] . "<p>";
 
/* Function to get the date of the first instance of a given day of a month of a year */
function GetFirstDay( $month, $x, $year )
{
    for ($day=1; $day<=7; $day++)
    {
        $t = mktime(0,0,0,$month,$day,$year);
        if (date('N',$t)==$x) return $t;
    }
}
 
$firstday = GetFirstDay($_POST["month"], $_POST["day"], $_POST["year"]);
echo date('Y-m-d', $firstday) . "<p>";
 
/* Function to get the number of days of a given month */
function GetDaysInMonth( $month )
{
    if ($month == 04) {
        $days = 30;
    }
    elseif ($month == 06) {
        $days = 30;
    }
    elseif ($month == 09) {
        $days = 30;
    }
    elseif ($month == 11) {
        $days = 30;
    }
    else {
        $days = 31;
    }
    return $days;
}
 
$days = GetDaysInMonth($_POST["month"]);
echo $days . "<p>";
 
/* Function to get the date of the last instance of a given day of a month of a year */
function GetLastDay( $month, $x, $year, $days )
{
    for ($day=$days; $day>=0; $day--)
    {
        $t = mktime(0,0,0,$month,$day,$year);
        if (date('N',$t)==$x) return $t;
    }
}
 
$lastday = GetLastDay($_POST["month"], $_POST["day"], $_POST["year"], $days);
echo date('Y-m-d', $lastday) . "<p>";
 
?>
However for two month values 'August' and 'September' the function 'GetDaysInMonth' and the form element reset control structures do not behave as expected and I cannot for the life of me understand why.

I'd very much appreciate someone looking at the code and explaining any mistakes I have made. If they could try recreating the problem I'd be very grateful. If someone only reports that the script works perfectly for them, that would also be great.

I'm a very new programmer using PHP 5.1.6 and have tried the script on two seperate Linux machines.

I've also just realised I've not accounted for February having 28 days, not 30 or 31. Hmmm.

Re: Date app not behaving as expected $_POST mktime 5.1.6

Posted: Sun Aug 17, 2008 11:37 am
by ghurtado
I'm not sure why the problem was only showing itself for those two months, but I fixed it by not using a leading zero, since that makes PHP treat your month numbers as strings. Try changing the pulldown to this:

Code: Select all

<select name="month">
<option value="1"<?php if ($_POST["month"] == 1) {echo " selected";} ?>>January</option>
<option value="2"<?php if ($_POST["month"] == 2) {echo " selected";} ?>>February</option>
<option value="3"<?php if ($_POST["month"] == 3) {echo " selected";} ?>>March</option>
<option value="4"<?php if ($_POST["month"] == 4) {echo " selected";} ?>>April</option>
<option value="5"<?php if ($_POST["month"] == 5) {echo " selected";} ?>>May</option>
<option value="6"<?php if ($_POST["month"] == 6) {echo " selected";} ?>>June</option>
<option value="7"<?php if ($_POST["month"] == 7) {echo " selected";} ?>>July</option>
<option value="8"<?php if ($_POST["month"] == 8 ) {echo " selected";} ?>>August</option>
<option value="9"<?php if ($_POST["month"] == 9) {echo " selected";} ?>>September</option>
<option value="10"<?php if ($_POST["month"] == 10) {echo " selected";} ?>>October</option>
<option value="11"<?php if ($_POST["month"] == 11) {echo " selected";} ?>>November</option>
<option value="12"<?php if ($_POST["month"] == 12) {echo " selected";} ?>>December</option>
</select>

Re: Date app not behaving as expected $_POST mktime 5.1.6

Posted: Sun Aug 17, 2008 4:31 pm
by rockatansky
August and September don't return properly because I was specifying my integers as octal; 08 and 09 don't exist.

Nice one ghurtado, thank you