Results 1 to 4 of 4

Thread: PHP math/Date calculation help (function).. pascal guys can probably help

  1. #1
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default PHP math/Date calculation help (function).. pascal guys can probably help

    PHP Code:
        function whatDate($date1) {

            
    $tots time() - strtotime(str_replace("-","/",$date1));

            if(
    $tots>31536000$tot round($tots/31536000,0).' year';

            else if(
    $tots>2628000$tot round($tots/2628000,0).' month';

            else if(
    $tots>604800$tot round($tots/604800,0).' week';

            else if(
    $tots>86400$tot round($tots/86400,0).' day';

            if(
    $tot>1$tot .= 's';

            return 
    $tot;

        } 
    Alright what I want to do:
    I want it to go up to 24 months and then start at 2 years x months // it currently goes to 12 months and then starts 1 year.. and then 2 years, no months

    I want it to be like:

    1 month
    2 months
    3 months
    ...
    24 months
    2 years
    2 years 1 month
    2 years 2 months
    ...
    3 years 1 month

    etc

    can anyone show me how to do that? I am calculating in seconds by the way.. all I am doing is grabbing

    2013-3-23 (yesterdays date for example)

    and using the exact current time/date to just get how many seconds

    I'm just confused on adding multiple things that output there


    (how it looks with not bad colors)
    Last edited by grats; 03-24-2013 at 07:59 PM.
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  2. #2
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Could you not give each part a seperate tot name, then return them all as one value?

    This might be useful too:

    http://php.net/manual/en/datetime.diff.php
    Last edited by xtrapsp; 03-25-2013 at 01:33 AM.

  3. #3
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    Could you not give each part a seperate tot name, then return them all as one value?

    This might be useful too:

    http://php.net/manual/en/datetime.diff.php
    and google couldn't even find a date difference for php

    <?php
    $dt1 = date_create('2010-04-01');
    $dt2 = date_create(date('Y-m-d'));
    $interval = date_diff($dt1,$dt2);
    echo $interval->format('%Y year %m months');


    ?>

    am I looking to do something like that being the best way?

    get on skype!

    revised:
    PHP Code:
    <?php

    $dt1 
    date_create('2010-05-25');
    $dt2 date_create(date('Y-m-d'));
    $interval date_diff($dt1,$dt2);
    $mdt $interval->format('%m');
    $ydt $interval->format('%y');
    $dtd $interval->format('%a');
    if(
    $dtd 730) {
        if(
    $ydt 1) {$ydt $ydt.' years ';}else if($ydt 1) {$ydt '';} else{$ydt $ydt.' year ';}
        if(
    $mdt 1) {$mdt $mdt.' months';}else if($mdt 1) {$mdt '';} else{$mdt $mdt.' month';}
        
    $age $ydt.$mdt.' old';
    }else {
        if(
    $dtd 1) {$ms ' days old';}else {$ms ' day old';}
        if(
    $dtd 30) {$dtd round($dtd/30.41666666671); 
            if(
    $dtd 1) {$ms ' months old';}else {$ms ' month old';}
    }
        
    $age $dtd.$ms;
    }
    echo 
    $age;

    ?>
    this does exactly what I want, is it the best way to do this?
    Last edited by grats; 03-25-2013 at 03:51 PM.
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  4. #4
    Join Date
    Oct 2011
    Posts
    422
    Mentioned
    15 Post(s)
    Quoted
    116 Post(s)

    Default

    ugh...

    what you are trying to do is to create a duration function.

    ..
    $mtime = time() - strtotime($date1);
    return duration($mtime);
    ..
    www.hawkee.com/snippet/2056/ <-- duration function


    strtotime is a string parser that converts any time rep human string into epoch time. Yes there is a rfc standard for it,

Thread Information

Users Browsing this Thread

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

Posting Permissions

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