Author Topic: Zend_Date Problems  (Read 2226 times)

0 Members and 1 Guest are viewing this topic.

Offline milesapTopic starter

  • Enthusiast
  • Posts: 64
    • View Profile
Zend_Date Problems
« on: April 06, 2009, 10:53:00 PM »
Hello,

I'm fairly new to Zend Framework, so forgive me if this is a really easy question. I have been looking everywhere for a solution. I am trying to get the difference between the current date and a persons birthday to see how old they are.

Code: [Select]
$date = new Zend_Date();
    $date->sub('April 13, 1987');

    print $date->toString('Y');

The following prints 28, however the correct age would be 29, as the persons birthday was a few days ago. Does it not take the month and day into account when it subs the date? Or a better question would be what am I doing wrong?

I have set my default timezone correctly.

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,587
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Zend_Date Problems
« Reply #1 on: April 06, 2009, 11:03:36 PM »
Subtracting two dates is the not definition of the age of anything. See this post - http://www.phpfreaks.com/forums/index.php/topic,246453.msg1152105.html#msg1152105
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline milesapTopic starter

  • Enthusiast
  • Posts: 64
    • View Profile
Re: Zend_Date Problems
« Reply #2 on: April 06, 2009, 11:58:21 PM »
I need to know how to tackle the problem using Zend_Date specifically, as I am learning that framework. In past projects I have used something similar:

Code: [Select]
function playersAge($birthDate) {
list($day, $month, $year) = explode("/", $birthDate);
$age = date('Y') - $year;
if (date('m') < $month) $age--;
elseif (date('d') < $day) $age--;
return $age;
}