0

I'm currently working on a little form that allows people to book a stay at a hostel. Basicly, I get them to pick two dates. (Day of arrival & day of departure) Then I want to calculate the difference between those dates in days, and finally I want to multiply the number of days with the price per day.

I can't seem to figure out how to calculate the days * price per day and pass that string on to the "amount value" in my form.

This is what I got so far:

<input type="text" id="datepicker" value="arrival (day-month-year)" />
<input type="text" id="datepicker2" value="departure (day-month-year)" />

<form action="checkout.php" method="post">
    <input name="amount" type="hidden" value="$dateDiff * price per day = value">
    <input type="submit" value="Check out" id="checkout">
</form>

And my PHP

<?php   
$date1 = ("#datepicker");
$date2 = ("#datepicker2");
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Difference is $fullDays days";  
?>

Edit: After the first two comments, let me explain that I'm fairly new to PHP. Thanks.

Jefferson
  • 993
  • 2
  • 16
  • 35
  • 1
    Try `echo $date1` and see that whatever you think you are doing there doesn't work. Once you get that fixed, you can start thinking about what you expect as result from subtracting one string from another, because that definitely won't do what you expect it to do. – Till Helge Mar 05 '13 at 08:54
  • I'm trying to catch the value of both #datepicker & #datepicker2. – Jefferson Mar 05 '13 at 08:56
  • 1
    That's not how you do it in PHP. Javascript, kind of, but not PHP – niaccurshi Mar 05 '13 at 08:57
  • 2
    Your form inputs aren't inside the form, you're not referencing the $_POST array variable which is where you'd access these values, you've not included name attributes on the two date fields. Go and look in to those and you'll find your answer :) – niaccurshi Mar 05 '13 at 08:59
  • Thanks Niaccurshi. That did the trick. It's exactly what HamZa explained. – Jefferson Mar 05 '13 at 09:07
  • Also, +5?? HOW?? I mean, when did this site become about unconstructive remarks about my poorly formulated question? – Jefferson Mar 05 '13 at 09:09

3 Answers3

2

The datetime object offers a diff function:

http://php.net/manual/en/datetime.diff.php

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
print_r($interval);

DateInterval Object ( [y] => 0 [m] => 0 [d] => 2 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 2 )

So you could calculate your price with

$price = $pricePerDay * $interval->days;
jantimon
  • 36,840
  • 23
  • 122
  • 185
2

You have to put the input fields in your form and give it a name likes this:

<form action="checkout.php" method="post">
    <input type="text" id="datepicker" name="datepicker" value="arrival (day-month-year)" />
    <input type="text" id="datepicker2" name="datepicker2" value="departure (day-month-year)" />
    <input name="amount" type="hidden" value="$dateDiff * price per day = value">
    <input type="submit" value="Check out" id="checkout">
</form>

Now in your PHP file:

<?php
  if(isset($_POST["datepicker"], $_POST["datepicker2"])){
    $date1 = new DateTime($_POST["datepicker"]);
    $date2 = new DateTime($_POST["datepicker2"]);
    $diff = $date1->diff($date2);
    $amount_per_day = 10; // for example 10 dollars
    $total = floor($diff["days"]) * $amount_per_day; // total
  }
?>
HamZa
  • 14,671
  • 11
  • 54
  • 75
1

Try this:

function dateDiff($start, $end)
{
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
Kabir
  • 2,126
  • 5
  • 21
  • 24