SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    accurate way to calculate time

    I am working on a project where a user can pick a date in this format '2013-06-30 12:00:00', the date is then stored for later usage.

    When the date is pulled the script calculates the time left to that specific date using strtotime and subtracting time to that then setting 6 different variables, the years, months, days, hour, minutes and seconds however I am getting varying results, if I reload the page the seconds may jump up instead of down.

    I am doing it based on dividing according to 60 * 60 * 24 * 30 * 365

    PHP Code:
    //$vars['target'] is the amount of seconds based on strtotime('2013-06-30 12:00:00')-time()
        
    if($vars['target'] > 31536000) {
            
    $years=floor($vars['target']/31536000);
            
    $vars['target']%=31536000;
        } else 
    $years=0;
        if(
    $vars['target'] > 2628000){
            
    $months=floor($vars['target']/2628000);
            
    $vars['target']%=2628000;
        } else 
    $months=0;
        if(
    $vars['target'] > 86400){
            
    $days=floor($vars['target']/86400);
            
    $vars['target']%=86400;
        } else 
    $days=0;
        if(
    $vars['target'] > 3600){
            
    $hours=floor($vars['target']/3600);
            
    $vars['target']%=3600;
        } else 
    $hours=0;
        if(
    $vars['target'] > 60){
            
    $minutes=floor($vars['target']/60);
            
    $vars['target']%=60;
        } else 
    $minutes=0;
        
    $seconds=$vars['target']; 
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I modifed the script completely, now I am using the datetime and everything is working as expected
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.


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
  •