find the no of days?

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
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

find the no of days?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Get the timestamps using strtotime(), then deduct one from the other to get the difference in seconds. The rest is easy.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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}";
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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
Last edited by PHPycho on Sat Feb 10, 2007 9:51 am, edited 1 time in total.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Anyway thanks to all of you...

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

Thanks in advance
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply