What's a smart way to count the number of days since X?

I started working at my current position since November 17th 2014. I would like to know how many days have run up to now. Any ideas on how to use Linux to come up with a simple and nice solution?

Asked By: ychaouche

||

Well, on the face of it:

$ date --date="-239 days"
Mon Nov 17 15:25:40 CET 2014

In a script (not very efficient, but… maybe it handles leap seconds? 😉 )

i=0
result=""
while [ "$result" != "20141117" ]
do
    i=$((i+1))
    result=$(date --date="-$i days" +%Y%m%d)
done
echo "$i" days have passed since "$result"
Answered By: frostschutz
echo $(( (`date +%s` - `date +%s -d '2014/11/17'`) / 86400 )) days ago
Answered By: wurtel

I tried python on the command line.

$ python -c "import datetime; print datetime.date.today() - datetime.date(2014,11,17)"
246 days, 0:00:00 
Answered By: ychaouche

Here is example with PHP:

<?php
$o1 = date_create('2014-11-17');
$o2 = date_create();
$o3 = date_diff($o2, $o1);
echo 'days: ', $o3->days, "n";

Result:

days: 1927

https://php.net/function.date-diff

Answered By: Zombo
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.