Subtracting PHP dates

Posted on 2011-03-21
Last Modified: 2012-06-27
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,
Question by:deanlee17
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35178941
there, you actually use the date() function to return the string in format YYYY-MM-DD (and with time HH24:MI:SS)
LVL 34

Accepted Solution

Beverley Portlock earned 500 total points
ID: 35178986
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

Author Comment

ID: 35179012
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'?

Industry Leaders: 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!

LVL 34

Expert Comment

by:Beverley Portlock
ID: 35179326
If I need to do calculations that are very precise I use Julian Dates 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

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";
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35179333
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";

Author Comment

ID: 35179343
Thats great, thanks very much :)

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question