Datediff

This plugin is used to calculate the numbers of days, months, or years between two dates just like datedif function in Microsoft Excel. You can use it to display your age dinamically, how long your blog has been online, or other events counter based on the dates.
 

Download
Click here to download datediff.zip.
 

Installation

  • Put the file "datediff.php" from extracted datediff.zip in your plugins folder. This is located at /wp-content/plugins/.
  • Go to the Plugins page in your WP Administration.
  • Activate Datediff plugin.
  • That's all, Datediff plugin is installed.

 

Usage
Now you have to choose where you want to use this date calculation on your blog. I suggest putting it wherever outside your post template such as sidebar, header, or footer. Then place this snippet:

PHP:
  1. <?php datediff(start_date, end_date, unit); ?>

 
start_date is a date represents starting date of the period.
end_date is a date represents ending date of the period, default "now".
The dates may be entered a string containing a US English date format according to the GNU Date Input Formats syntax. Some valid value are "2005-08-24", "24 August 2005", "now", "tomorrow", "now +7 hours", or "next Thursday".

unit is the type of information you want returned as explained below:

unit Returns
"Y" The number of complete years in the period.
"M" The number of complete months in the period.
"D" The number of complete days in the period.
"MD" The difference between the days in start_date and end_date. The months and years of the dates are ignored.
"YM" The difference between the months in start_date and end_date. The days and years of the dates are ignored.
"YD" The difference between the days in start_date and end_date. The years of the dates are ignored.

The default value for unit is "D".
 

Examples

CODE:
  1. I have my birthday on next <?php datediff("2005-9-22"); ?> days.

output:
I have my birthday on next 27 days.

CODE:
  1. I am <?php datediff("1980-8-26","now","y"); ?> years old.

output:
I am 25 years old.

CODE:
  1. This blog have been online for
  2. <?php datediff("2005-7-18","now","y"); ?> years,
  3. <?php datediff("2005-7-18","now","ym"); ?> months
  4. and <?php datediff("2005-7-18","now","md"); ?> days.

output:
This blog has been online for 0 years, 1 months and 10 days.

38 Responses to “Datediff”

Robinik - Gravatar

Robinik said on August 30, 2005, 10:02 pm:

TY ;)

I put it in my Blog.

Rob

Claire - Gravatar

Claire said on September 1, 2005, 8:04 am:

For the “Usage” code, you accidentally typed “datedif” instead of “datediff”.

maseko - Gravatar

maseko said on September 1, 2005, 3:34 pm:

Thanks Claire, the mistake has been corrected.

phil ws - Gravatar

phil ws said on October 31, 2005, 3:16 pm:

can i enter a B.C. date ? tnx

maseko - Gravatar

maseko said on October 31, 2005, 6:26 pm:

#4
According date() and strtotime() function in PHP, the date limited from 01-01-1970 to 19-01-2038.

tulek - Gravatar

tulek said on December 2, 2005, 5:51 am:

Nice plugin :). Love it!

Tikomi - Gravatar

Tikomi said on December 4, 2005, 10:59 pm:

Cool plugin. Just placed in my blog to display days ’till birthday. Now, my friends won’t forget it. Thanks.

Adam - Gravatar

Adam said on January 23, 2006, 5:00 pm:

Put it in the “Author” section of my sidebar. thx.

Sushigami » - Gravatar

Sushigami » said on January 24, 2006, 6:55 am:

[...] I decided I wanted to have the posts show the day of the project on which they were posted. I think it feels more dramatic to know that this is say ‘Day 26′ of the project than to know it is 23rd Jan 2006 (which incidently is election day in Canada.) To achieve this, I downloaded a plugin called datediff and then used the following magic incantation in the post.php file of my theme [...]

Chris - Gravatar

Chris said on February 6, 2006, 9:19 pm:

Nice plugin - many thanks!

Chris

irma- - Gravatar

irma- said on March 6, 2006, 6:40 pm:

this is great, thanks! =)

SingingLarvae - Gravatar

SingingLarvae said on March 23, 2006, 6:07 pm:

Excellent ! Just what I was looking for !

Thx

CuttyP - Gravatar

CuttyP said on April 5, 2006, 11:05 pm:

Very nice, now I always know how many days ago I’ve been established ;)

Basudeb Diasi - Gravatar

Basudeb Diasi said on April 6, 2006, 5:33 pm:

it was more important for me. it is outstanding funtion which i am looking for.

Helmar - Gravatar

Helmar said on April 11, 2006, 3:39 pm:

thanks for the code!
I use the datediff function to check if a document is expired or not, so i added a new variable $sign to the code to see, if ($start > $end) or not.
Since i don’t use the file datediff.php as a WP plugin but within my apache, i changed all the “print”s to “return”s, so i can use it with:

include “datediff.php”;
$expires=datediff(”now”,$expdate);

Maybe this is also usefull to other user.

~$ diff datediff.php datediff.php.orig
33c33
print(”invalid start date”);
38c38
print(”invalid end date”);
45d44
print(intval($diff/(24*60*60)));
67c66
print($mdiff);
75c74
print($ydiff);
91c90
print($ymdiff);
99c98
print($yddiff);
107c106
print($mddiff);
110c109
print(”{Datedif Error: Unrecognized \$unit parameter. Valid values are ‘Y’, ‘M’, ‘D’, ‘YM’. Default is ‘D’.}”);

MatrixMonkey - Gravatar

MatrixMonkey said on July 8, 2006, 11:03 pm:

Just the thing i been looking for spent ages lookin for somthing to count days from day X
used it on my site :D

jeroen - Gravatar

jeroen said on July 11, 2006, 3:16 am:

Nice plugin! Only i was wondering if you could help me out with the following thing. I wat to display the difference (in days) between a static date and the postingdate?

The second date is defined like this

Thanks in advance & keep up the good work!

kiki - Gravatar

kiki said on August 6, 2006, 12:03 am:

Super plugin!! Thank you!!

Andrew - Gravatar

Andrew said on November 13, 2006, 8:34 am:

Brill - just what I was looking for. Thanks!

Karl - Gravatar

Karl said on December 8, 2006, 6:05 pm:

Hi,

Thanks for a great plugin. I have a question though. It works fine on the sidebar of my website, but i want to use it _inside_ pages. The same code, won’t output anything inside pages. Is there a work-around?

Cheers,
Karl

maseko - Gravatar

maseko said on December 8, 2006, 6:34 pm:

Hi Karl,

You need additional plugin like PHP Exec Plugin to execute PHP code inside pages/entries.

My Thoughts » Part 2 - Gravatar

My Thoughts » Part 2 said on December 25, 2006, 12:21 am:

[...] Also, all this morning I have been working on my sidebar. A friend and fellow postie (Meredith of Amanita.net was helping me. I was able to install a plugin called DateDiff. Right now I just have it displaying the age of my blog. I plan to play with it more and get some other stuff in there. [...]

subramanian - Gravatar

subramanian said on January 5, 2007, 2:33 pm:

mgkn dapat membantu utk webblog saya, thx

Ravetracer - Gravatar

Ravetracer said on February 7, 2007, 2:56 pm:

Hello there.
Many thanks for that script. I wrote a ticker with the lifetime of
my son and my daughter. In every month-changing, one of them has
one day more or less, than the other one, but your function works
very well now. Good work.

there is a bug - Gravatar

there is a bug said on February 24, 2007, 3:18 am:

try datediff(’03/10/2007′, ‘03/12/2007′, ‘D’)
it should be 2, but 1.

Could let me know how is that?

Serdar - Gravatar

Serdar said on March 27, 2007, 5:55 pm:

There are problem in script but I didn’t solve where it is .
example=datediff(2007-03-01,2007-03-27,D)
result=25
real =26

maseko - Gravatar

maseko said on March 27, 2007, 6:25 pm:

Thank you, I will ceck it.

dereinzige - Gravatar

dereinzige said on April 6, 2007, 12:21 pm:

i put it in my footer pointing to the *end of time* ;)

thanks alot … greetings and love to the other side of our planet

rizal - Gravatar

rizal said on April 9, 2007, 11:39 pm:

@maseko: tengkyu bangat yaa..
dah sharing function ini.. hari ini kantor gw kena musibah.. si DBADMIN ngaco, tau2 server mysql id specdown tanpa alasan ke 4.0
jadinya script2 pada error smua.

WP Plugins DB » Blog Archive » Datediff - Gravatar

WP Plugins DB » Blog Archive » Datediff said on May 20, 2007, 2:46 pm:

[...] Visit [...]

Johncena - Gravatar

Johncena said on June 2, 2007, 11:34 am:

How to install this plugin. i have xampp installed. where is the plugins folder????

Johncena - Gravatar

Johncena said on June 2, 2007, 11:35 am:

How to install this plugin? i have xampp installed. where is the plugins folder????

Dwi susanto - Gravatar

Dwi susanto said on June 4, 2007, 11:43 am:

rtgrfyh

Victor - Gravatar

Victor said on June 8, 2007, 5:32 am:

Thanks, just what i was lookig for! thank a lot !speo club cristal and speologie

Frank Luas - Gravatar

Frank Luas said on December 27, 2007, 1:33 am:

Your plugin will work on dates before 1970. The error it generated when I attempted to use a 1960s date was:

ERROR: Warning: date() [function.date]: Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in D:\Domains\sunrisedancer.com\www\weathervane\wp-content\plugins\datediff.php on line 49

regards.

julien - Gravatar

julien said on February 7, 2008, 12:50 am:

can we do the same with javascript ? thx

rochee - Gravatar

rochee said on May 7, 2008, 5:13 pm:

do you know the syntax of getting the datediff of the current date and a date linked a database access…?please help me.

datediff(”d”,[tbl_inventory.Field!dateAcquired],Now ) … ?
dont know how.

Leave A Comment

All fields marked with "*" are required.