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
  • 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'?

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

828 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