Skip to main content
Participating Frequently
November 1, 2010
Answered

Time Comparison PHP

  • November 1, 2010
  • 1 reply
  • 945 views

Hi There


I am trying to create a time comparison for a website I am building.


I have the following so far

<?php

$date=$_POST ['daydropdown'];

$month=$_POST ['monthdropdown'];

$year=$_POST ['yeardropdown'];

$datevalue="$date/$month/$year";

$booking=strtotime ("now + 24 hours");

if ($date_value > $booking)

echo ("lovey");

else

echo ("tut tut");

?>

which is pulling its date info from

          <form action="timetest.php" method="post">

          <table>

  <tr>

    <td class="tableheight">

      Party Size</td>

    <td>

      <select name="partysize" size="1" id="partysize">

      <option>1</option>

      <option selected="selected">2</option>

      <option>3</option>

      <option>4</option>

      <option>5</option>

      <option>6</option>

      <option>7</option>

      <option>8</option>

      <option>9</option>

      <option>10</option>

      <option>11</option>

      <option>12</option>

      <option>13</option>

      <option>14</option>

      <option>15</option>

      <option>16</option>

      <option>17</option>

      <option>18</option>

      <option>19</option>

      <option>20</option>

    </select>

    </td>

  </tr>

  <tr>

    <td class="tableheight">Date</td>

    <td>

<select name="daydropdown" id="daydropdown">

</select>

<select name="monthdropdown" id="monthdropdown">

</select>

<select name="yeardropdown" id="yeardropdown">

</select>

</td>

  </tr>

  <tr>

    <td class="tableheight">Time</td>

    <td><select name="time">

      <option>12 noon</option>

      <option>12:15pm</option>

      <option>12:30pm</option>

      <option>12:45pm</option>

      <option>1:00pm</option>

      <option>1:15pm</option>

      <option>1:30pm</option>

      <option>1:45pm</option>

      <option>2:00pm</option>

      <option>2:15pm</option>

      <option>2:30pm</option>

      <option>2:45pm</option>

      <option>3:00pm</option>

      <option>3:15pm</option>

      <option>3:30pm</option>

      <option>3:45pm</option>

      <option>4:00pm</option>

      <option>4:15pm</option>

      <option>4:30pm</option>

      <option>4:45pm</option>

      <option>5:00pm</option>

      <option>5:15pm</option>

      <option>5:30pm</option>

      <option>5:45pm</option>

      <option>6:00pm</option>

      <option>6:15pm</option>

      <option>6:30pm</option>

      <option>6:45pm</option>

      <option>7:00pm</option>

      <option>7:15pm</option>

      <option>7:30pm</option>

      <option>7:45pm</option>

      <option>8:00pm</option>

      <option>8:15pm</option>

      <option>8:30pm</option>

      <option>8:45pm</option>

      <option>9:00pm</option>

      <option>9:15pm</option>     

</select></td>

  </tr>

  <tr>

    <td class="tableheight">First Name</td>

    <td><input name="firstname" type="text" placeholder="Your First Name" /></td>

  </tr>

  <tr>

    <td class="tableheight">Surname</td>

    <td><input name="surname" type="text" placeholder="Your Last Name"/></td>

  </tr>

  <tr>

    <td class="tableheight">Email Address</td>

    <td><input name="email" type="email" placeholder="Your Email Address"/></td>

  </tr>

  <tr>

    <td class="tableheight">Confirm Email </td>

    <td><input name="confirm" type="email"  placeholder="Confirm Email"/></td>

  </tr>

  <tr>

    <td class="tableheight">Contact Number</td>

    <td><input name="contactnumber" type="text" placeholder="Your Contact Number"/></td>

  </tr>

  <tr>

    <td style="padding-top:0px"><input name="Book Now" type="submit" value="Book Now" /></td>

    <td></td>

  </tr>

</table>

          </form>

          <script type="text/javascript">

//populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)

window.onload=function(){

populatedropdown("daydropdown", "monthdropdown", "yeardropdown")

}

</script>

What I want to happen is if a booking for a table is made for more than 24hours in advance then its fine and sends the reservation to the restaurant aka "Lovely" where as if its for within the next 24 hours then "tut tut" they are told to ring the restaurant.

Does anyone have any pointers for me please.

G

This topic has been closed for replies.
Correct answer UteFanJason

I have toyed around with it and I think it actually has to do with the date formatting you are using. If the date I pass to string to time is using words for the month (01/January/2011), like January, strtotime runs into an error.

If instead you pass it all numbers for the date (01/01/2011), it will work just fine.

So, if your form passes the month as a word rather than the numeric representation (01 for Jan, 02 for Feb) then you need to convert the month from text to number before putting it into the strtotime function.

Good luck.

1 reply

UteFanJason
Inspiring
November 1, 2010

You need to change the $datevalue variable to a timestamp like you did with $booking.

$dateTried = strtotime($datevalue);

Then you can compare them.

if ($dateTried < $booking) {

     // this is what to do if the time chosen was within 24 hours

} else {

     // this is what to do if the time chosen was a valid value (24 hours or more from current time)

}

BTW, it looked like your form fields are populated using JavaScript. If I were you I would switch that to be PHP driven. The code will be very similar in PHP as your JavaScript code, but it will work for all your visitors, not just the ones that have JavaScript enabled.

-Jason

Sorry the code didn't line up. I used the wrong code insert options.

Message was edited by: UteFanJason

BigG2009Author
Participating Frequently
November 2, 2010

Hi There


Thanks for that

I have used

<?php

$date=$_POST ['daydropdown'];

$month=$_POST ['monthdropdown'];

$year=$_POST ['yeardropdown'];

$datevalue="$date/$month/$year";

$booking=strtotime ("now + 24 hours");

$dateTried = strtotime($datevalue);

if($dateTried < $booking) {

echo ("tut tut");

}

else {

echo ("that will ding dang do for us");

}

?>

But its still just giving me the one echo of tut tut even when I make a booking for later.

One day PHP will make sense to me.

G

UteFanJason
UteFanJasonCorrect answer
Inspiring
November 2, 2010

I have toyed around with it and I think it actually has to do with the date formatting you are using. If the date I pass to string to time is using words for the month (01/January/2011), like January, strtotime runs into an error.

If instead you pass it all numbers for the date (01/01/2011), it will work just fine.

So, if your form passes the month as a word rather than the numeric representation (01 for Jan, 02 for Feb) then you need to convert the month from text to number before putting it into the strtotime function.

Good luck.