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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

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!

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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