Difference between 2 timestamps always a day extra

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Difference between 2 timestamps always a day extra

Post by anjanesh »

I want to find the number of days in-between two timestamps and date() is always return a day extra.

Code: Select all

echo strtotime("2005-01-04").'<br>';
echo strtotime("2005-01-04").'<br>';
echo date("d",strtotime("2005-01-04")-strtotime("2005-01-04")).'<br>';
This should return 0 days but returns 1.
How is this possible ?
Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

actually, it is date("d", 0) that returns 01

Code: Select all

echo strtotime("2005-01-04")-strtotime("2005-01-04");
echo "<br>";
echo date("d", 0);
outputs:

0
01
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

short explanation of why it happens: 'd' is the current day of the timestamp provided. A timestamp of zero is January 1st, 1970. If you google the "unix epoch" you should find further explanations, although they may not be satisfying.
Post Reply