Results 1 to 4 of 4

Thread: Date Calculations

  1. #1
    brodellis is offline Junior Member
    Join Date
    Nov 2006
    Posts
    4

    Default Date Calculations

    I'm trying to implement a logichook for Tasks module. I'm simply trying to get the difference between the start_date and end_date of the tasks. I'm new to PHP and SugarCRM so if there is an internal function that I can use please let me know. Any guidance provided is greatly appreciated.

    The section of code in question is highlighted in red below.

    Thanks,
    Brod

    Version: 5.1.0b
    Windows
    MySQL 5.0.51b


    <?php
    function lookup_year_week(&$year_num, &$week_num, $start_date){
    //try if this can obtain db connection, might not work
    global $timedate;
    $db_date = $timedate->swap_formats($start_date, $timedate->get_date_format(), $timedate->dbDayFormat);

    $db = DBManagerFactory::getInstance();
    $query = 'select year, week from week_period where start_date <=\''. $db_date . '\' AND end_date >=\''. $db_date . '\'';
    $result = $db->query($query, true, $app_strings['ERR_EXPORT_TYPE'].$type.": <BR>.".$query);
    $row = $db->fetchByAssoc($result, -1, false);
    $year_num = $row['year'];
    $week_num = $row['week'];

    }


    function date_diff($end_date, $start_date){
    error_log($end_date . " " . $start_date . "\r\n", 3, "bellis.log");

    $timedate = new




    $dateDiff = $end_date - $start_date;

    error_log($dateDiff . "\r\n", 3, "test.log");
    $fullDays = floor($dateDiff/(60*60*24));
    $fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
    $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);

    error_log($fullMinute, 3, "bellis.log");
    return $fullMinutes;
    }

  2. #2
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: Date Calculations

    Hi brodellis

    You can use the datediff built in MySQL date function.
    To deal with the calculated date you need to fill some $focus attribute with this calculated value.
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  3. #3
    brodellis is offline Junior Member
    Join Date
    Nov 2006
    Posts
    4

    Default Re: Date Calculations

    I've figured out how to retrieve the two date values in the following formats:

    Field 1: 1/22/2009 10:30:00 PM

    Field 2: 1/22/2009 11:00:00 PM

    The question I have now is how do I calculate the difference between the two times.

    Forgive me if this is a basic question, but I"m new to PHP and SugarCRM and need a bit of guidance.

    Thanks

  4. #4
    mvngti is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    South Africa
    Posts
    510

    Default Re: Date Calculations

    Quote Originally Posted by brodellis View Post
    Field 1: 1/22/2009 10:30:00 PM
    Field 2: 1/22/2009 11:00:00 PM
    The question I have now is how do I calculate the difference between the two times.
    PHP Code:
    $diff_in_seconds strtotime($field1) - strtotime($field2);
    $diff_in_hours floor((strtotime($field1) - strtotime($field2))/3600);
    $diff_in_days floor((strtotime($field1) - strtotime($field2))/86400); 
    --


    Marnus van Niekerk

    There are only 10 types of people in the world
    those who can read binary and those who don't

    Modules:
    CE Teams - Upgrade safe teams module for Community Edition
    FieldACL - Field Level Access Control for Community Edition
    EditLogicHooks - Create and edit Logic Hooks from the Admin GUI
    FlexibleChartDashlet - Display any data in a Dashlet Chart
    DocumentThumbnails - Thumbnails for Documents module

    Many questions can be answered by reading the Developers Manual

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculations in reports
    By REByers in forum Help
    Replies: 5
    Last Post: 2010-03-01, 04:22 AM
  2. How to add calculations to a module
    By vanrechemh in forum Help
    Replies: 4
    Last Post: 2009-10-20, 08:49 AM
  3. Mathematical calculations
    By danrweki in forum Developer Help
    Replies: 2
    Last Post: 2008-08-05, 07:58 AM
  4. Steam Turbine Calculations
    By nonstops in forum Help
    Replies: 0
    Last Post: 2007-10-17, 08:07 AM
  5. Help with Calculations
    By JackieBM in forum Help
    Replies: 0
    Last Post: 2007-02-21, 06:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •