Link to home
Start Free TrialLog in
Avatar of Dennie
Dennie

asked on

days hours left

Hi,

I want to now how to get the days and hours left to a certain date from now. This is what i got right now..

      $nowstr =  strtotime("now");
      $expirestr = strtotime($transaction->expire_ts);
      $daysleft = ($expirestr - $nowstr) / 86400;
SOLUTION
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pnorms
pnorms

I use this:
<?php
// 0, 0, 0, 9, 25, 2008 is Midnight Jan, 1st 2012
$targetdate = mktime(0, 0, 0, 1, 1, 2012) ;
$now = time () ;
$difference =($targetdate-$now) ;
$days =(int) ($difference/3600) ;
print "Time to New Years $days hours";
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may be able to get some good ideas from this article:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The PHP function GregoriantoJD() will give you a day count.  For greater precision, (hours, minutes, seconds) you might do a similar thing with the understanding that each of these (and weeks, too) have a predictable number of seconds.  Years and months do not.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points - it's an interesting question.  As a result of working on it, I updated the article here:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The new version has not been published yet, but this is the code example I used.

Best regards, ~Ray
function elapsed($then, $wdhms='WDHMS', $return_array=FALSE)
{
    // TIME CONSTANT VALUES (MONTHS AND YEARS ARE OMITTED - NOT FIXED VALUES)
    $seconds_per["W"] = 60*60*24*7;
    $seconds_per["D"] = 60*60*24;
    $seconds_per["H"] = 60*60;
    $seconds_per["M"] = 60;
    $seconds_per["S"] = 1;

    // TIME DESCRIPTIVE TERMS - YOU CAN USE SOME OR ALL OF THESE
    $terms["W"] = 'week';
    $terms["D"] = 'day';
    $terms["H"] = 'hour';
    $terms["M"] = 'minute';
    $terms["S"] = 'second';

    // SET THE FLAGS FOR OUTPUT
    if (empty($wdhms)) $wdhms = 'WDHMS';
    $wdhms = strtoupper($wdhms);
    $wdhms = str_split($wdhms);
    $wdhms = array_combine($wdhms, $wdhms);

    // SET THE FUTURE/PAST TIMESTAMP OR FAIL ON ERROR
    if (!$other_timestamp = strtotime($then)) return FALSE;

    // SET THE CURRENT TIMESTAMP
    $current_timestamp = time();

    // COMPUTE THE DIFFERENCE IN SECONDS
    $lapsed_seconds    = $other_timestamp - $current_timestamp;
    if ($lapsed_seconds == 0) return 'RIGHT NOW!';

    // DETERMINE FUTURE OR PAST
    $since_until = "until";
    if ($lapsed_seconds < 0)
    {
        $since_until = "since";
        $lapsed_seconds = abs($lapsed_seconds);
    }

    // COMPUTE THE INCREMENTS
    foreach ($seconds_per as $key => $secs)
    {
        if (array_key_exists($key, $wdhms))
        {
            $lapsed_time    = floor($lapsed_seconds / $secs);
            $lapsed_seconds = $lapsed_seconds - ($lapsed_time * $secs);
            $wdhms[$key]    = (int)$lapsed_time;
        }
    }

    // RETURN AN ARRAY
    if ($return_array) return $wdhms;

    // RETURN A RESPONSE STRING
    $s = NULL;
    foreach ($terms as $key => $str)
    {
        // RETURN ONLY THE UNITS THAT WERE REQUESTED IN $wdhms
        if (!array_key_exists($key, $wdhms)) continue;
        $s
        .= ' '
        . $wdhms[$key]
        . ' '
        . $str
        ;
        if ($wdhms[$key] != 1) $s .= 's';
        $s .= ',';
    }
    
    // TIDY UP THE RETURN STRING AND SHOW UNTIL OR SINCE
    $s = rtrim($s, ',');
    $s .= " $since_until $then";
    $s = trim($s);
    return $s;
}

// USAGE EXAMPLES
$then = '+ 1 days';
echo PHP_EOL . elapsed($then, 'h');
echo PHP_EOL . elapsed($then, 'm');
echo PHP_EOL . elapsed($then, 's');
$then = '+1304 hours +2917 seconds';
echo PHP_EOL . elapsed($then);
echo PHP_EOL . elapsed($then, 'WD');
echo PHP_EOL . elapsed($then, 'DH');
echo PHP_EOL . elapsed($then, 'HMS');
echo PHP_EOL;
$arr = elapsed($then,NULL,TRUE);
var_dump($arr);

Open in new window