Page 1 of 1

find the no of days?

Posted: Sat Feb 10, 2007 7:49 am
by PHPycho
situation:
$date1 = date("Y-m-d");
$date2 = "2007-01-12";

problem:
how to find the no of days between $date1 and $date2 ?
Any help is appreciated...
Thank you

Posted: Sat Feb 10, 2007 9:07 am
by Chris Corbyn
Get the timestamps using strtotime(), then deduct one from the other to get the difference in seconds. The rest is easy.

Posted: Sat Feb 10, 2007 9:09 am
by impulse()
This may work:

Code: Select all

$date1A = strtotime($date1);
$date2A = strtotime($date2);

$total = $date2A - $date1A;

$AOD = $total / 86400;

echo "Amount of days is {$total}";

Posted: Sat Feb 10, 2007 9:20 am
by PHPycho
Hey i got the solutions

solution 1: using php

Code: Select all

echo (strtotime($date1) - strtotime($date2)) / 86400;
solution 2: using mysql

Code: Select all

select datediff(now(), '2007-01-12') as days

Posted: Sat Feb 10, 2007 9:22 am
by PHPycho
Anyway thanks to all of you...

again one question:
which one is preferrable using solution of PHP or Mysql ?

Thanks in advance

Posted: Sat Feb 10, 2007 9:23 am
by RobertGonzalez
If you would have told us the dates were in the database we could have told you to use MySQL functions to handle the date math.

Keep in mind your first solution doesn't return days it returns seconds.