Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Subtracting PHP dates

Hi Experts,

I needed to subtract an arrival date from a departure date to work out the number of nights a customer stayed. So I used

$Arrival_Date = strtotime($Arrival_Date);
$Departure_Date = strtotime($Departure_Date);
$NoNights = ($Departure_Date - $Arrival_Date) / 86400;

This seemed to work fine, however I now need to put those 2 variables back into the correct format to be stored in my MySql table. How can I do this? and secondly a friend said I should be using MkDate to do this kind of thing, is this true?

Many Thanks,
Dean
0
deanlee17
Asked:
deanlee17
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
there, you actually use the date() function to return the string in format YYYY-MM-DD (and with time HH24:MI:SS)
http://php.net/manual/en/function.date.php
0
 
Beverley PortlockCommented:
OK, you don't need to convert the dates back to YYY-MM-DD format if you don't overwrite them in the first place. So instead of

$Arrival_Date = strtotime($Arrival_Date);
$Departure_Date = strtotime($Departure_Date);
$NoNights = ($Departure_Date - $Arrival_Date) / 86400;

Open in new window


do this

$A_d = strtotime($Arrival_Date);
$D_d = strtotime($Departure_Date);
$NoNights = ($D_d - $A_d) / 86400;

Open in new window


"]a friend said I should be using MkDate to do this kind of thing, is this true?

No, it is not true. I find mkdate clumsy to use and as a result I very, very rarely use it. date and strtotime manage nearly all my needs
0
 
deanlee17Author Commented:
bportlock, thats superb, thanks very much for this.

I think he said the method above struggles on leap yours or something like that. So what occasions would you actualyl use mkdate over your preferred method of 'date' and 'strtotime'?

Thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Beverley PortlockCommented:
If I need to do calculations that are very precise I use Julian Dates http://uk3.php.net/manual/en/function.gregoriantojd.php but for 99% of my programming needs the leap year will make no difference. Most people accept vague terms like "Next Month" which can be virtually anything from 1st of next to the 31st.

strtotime is frighteningly flexible - look at these examples from http://www.php.net/strtotime

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
0
 
Beverley PortlockCommented:
Typos in the examples above corrected below

echo "<pre>\n";
echo strtotime("now") . "\n";
echo strtotime("10 September 2000") . "\n";
echo strtotime("+1 day") . "\n";
echo strtotime("+1 week") . "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds") . "\n";
echo strtotime("next Thursday") . "\n";
echo strtotime("last Monday") . "\n";
echo "</pre>\n";
0
 
deanlee17Author Commented:
Thats great, thanks very much :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now