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.

55 Responses to “Datediff”

Robinik - Gravatar

www.robinik.net Robinik said on August 30, 2005, 10:02 pm:

TY ;)

I put it in my Blog.

Rob

Claire - Gravatar

www.weirdncrazy.com/universe/ Claire said on September 1, 2005, 8:04 am:

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

maseko - Gravatar

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

Thanks Claire, the mistake has been corrected.

phil ws - Gravatar

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

can i enter a B.C. date ? tnx

maseko - Gravatar

maseko.com 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.net tulek said on December 2, 2005, 5:51 am:

Nice plugin :) . Love it!

Tikomi - Gravatar

tikomi.org/ 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

www.adam.haaparanta.se Adam said on January 23, 2006, 5:00 pm:

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

Sushigami » - Gravatar

sushigami.com/wordpress/?p=6 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

www.r1150r.ch Chris said on February 6, 2006, 9:19 pm:

Nice plugin – many thanks!

Chris

irma- - Gravatar

irmetta.free.fr/ 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

www.cuttyp.de 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

www.gerloni.net/ 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.stuffabout.me.uk 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

avontuur.steenvoorde.org 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

wongablog.co.uk Andrew said on November 13, 2006, 8:34 am:

Brill – just what I was looking for. Thanks!

Karl - Gravatar

www.malasse.dk 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.com 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

willifordblog.com/?p=375 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

shjk.blogspot.com subramanian said on January 5, 2007, 2:33 pm:

mgkn dapat membantu utk webblog saya, thx

Ravetracer - Gravatar

ravetracer.castarnie.de 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.com maseko said on March 27, 2007, 6:25 pm:

Thank you, I will ceck it.

dereinzige - Gravatar

dereinzige.de 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

www.wp-plugins-db.org/plugin/datediff/ 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_ikplasa.com Dwi susanto said on June 4, 2007, 11:43 am:

rtgrfyh

Victor - Gravatar

www.speocristal.ro 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.

adam - Gravatar

solusidesain.com adam said on June 11, 2008, 11:49 am:

wah makasih mas eko…

ini yg saya cari :)

Fadli - Gravatar

www.locjan.com Fadli said on June 14, 2008, 9:10 am:

wah nggak ada terjemahan indonya agak lama nih menghayatinya, hehe.

Bambang Eko - Gravatar

bambangoke.blogspot.com Bambang Eko said on July 17, 2008, 9:13 am:

Bagus….
Ini buat ngitung jumlah hari jg g?

pkhrjx evboyui - Gravatar

www.ebdnp.daepv.com pkhrjx evboyui said on October 21, 2008, 4:09 pm:

uzdafgqt tyqs wrevq slor umdxj aqhj qkcvjl

Wong Edan-Tong Bolong - Gravatar

situsirfan.co.cc Wong Edan-Tong Bolong said on October 24, 2008, 3:39 pm:

#All comments using English, Why??????
#Semua komentar menggunakan Bahasa Inggris, Mengapa??????
#Kabeh unek-unek nganggo boso Inggris, Ngopo??????
#ibafa aduaghfiug adfabifb ajkdb, qerqr??????
#1871 474773 2299 19082 128, 3698584??????
#%@*&%^$%# ^#}%&*& *&%#!!!>< &%^ *%675, *(%$#??????

Wong Edan-Tong Bolong - Gravatar

situsirfan.co.cc Wong Edan-Tong Bolong said on October 24, 2008, 3:41 pm:

Sorry Mister, about my comment above.

wong waras - Gravatar

wong.com wong waras said on November 18, 2008, 8:19 am:

@wong edan
bilang aja elu kagak bisa bahasa inggris,hehehe bercanda

WordPress Wednesday: Current Plugins — davidgagne.net - Gravatar

www.davidgagne.net/2009/02/18/wordpress-wednesday-current-plugins/ WordPress Wednesday: Current Plugins — davidgagne.net said on February 18, 2009, 11:14 pm:

[...] DateDiff – used to calculate age of posts and how long the site’s been up (see bottom of page) [...]

WordPress Wednesday: How to Display the Age of Your Site — davidgagne.net - Gravatar

www.davidgagne.net/2009/02/25/wordpress-wednesday-how-to-display-the-age-of-your-site/ WordPress Wednesday: How to Display the Age of Your Site — davidgagne.net said on February 25, 2009, 11:16 pm:

[...] been publishing this site. It’s pretty simple to do this; all you really need is the DateDiff plugin and PHP’s default date() [...]

mamoru - Gravatar

mamoru said on March 17, 2009, 4:08 pm:

mas eko thx alot ntar aq nanya2 nih..he..he…

Ali - Gravatar

hera-ku.blogspot.com Ali said on April 11, 2009, 11:50 am:

mas,, aku mau belajar wordpress.. tapi gimana caranya

?y?y??? - Gravatar

lysenkovshina.ru ?y?y??? said on May 28, 2009, 7:48 pm:

????? ??? ???????????, ??-?????????? ???????? ??????????.

Datediff - Gravatar

www.wp-plugin-archive.de/2009/06/19/datediff/ Datediff said on June 19, 2009, 5:13 pm:

[...] you are new here, you might want to subscribe to the RSS feed for updates on this topic.Das Plugin Datediff von Maseko ermittelt anhand der Datenn der Postings, wie lange das Blog schon exisiert und zeigt [...]

takien - Gravatar

takien.com takien said on November 15, 2009, 4:22 pm:

THanks… it’s simple yet useful

Affan Rifaki Statistisi Merakyat - Gravatar

www.rifaki.info/ Affan Rifaki Statistisi Merakyat said on January 17, 2010, 9:35 pm:

I see it.
Your explanation is very smart.

panga - Gravatar

hephoz.de panga said on January 23, 2010, 1:32 am:

Maybe something to know if you want to show the number of days to an upcoming event. For example: today is 2010-1-22 and the event is tomorrow (2010-1-23).

Using datediff(”2010-1-23″) will result in “0″ but usually you want to display “1″ (days left).

It’s not a “bug” because your function shows COMPLETE days to an event. That’s what you write in your USAGE.

Maybe it’s better to return the value in your function rather than printing it immediately so you can use your function for further calculation. In the example above people may want to add a day to the result but (datediff(”2010-1-23″)+1) will not work. If you return the value users could use

echo datediff(”2010-1-23″,D)+1;

to show an upcoming event as explained above.

h3ru - Gravatar

heru-spt.co.cc h3ru said on February 5, 2010, 3:59 pm:

Makasih Mas Eko,
minta ijin untuk menggunakan di blog saya…

tks :D

Leave A Comment

All fields marked with "*" are required.