• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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;
0
Dennie
Asked:
Dennie
  • 3
  • 2
3 Solutions
 
Rik-LeggerCommented:
I found this by searching:

$diff = $ends-$now;

$days = intval($diff/24/60/60);
$remain=$diff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60); 

Open in new window

http://www.webmasterworld.com/php/3019880.htm
0
 
pnormsCommented:
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

0
 
pnormsCommented:
Just cam up with this: A bit better
<?php
	$todaysdate=date("Ymd"); //Year 2011, Month 04, Day 02
	$targetdate="20110402";
	$daysleft=($targetdate-$todaysdate);
	$timenow=date(Hi); //24 Hour Format
	$targettime="1430";
	$timeleft=($targettime-$timenow);
	$timeleft=substr_replace($timeleft, ".", -2, 0);
	echo "<br>Time To Date: $daysleft Days, $timeleft hours<br>";
?>

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Ray PaseurCommented:
You may be able to get some good ideas from this article:
http://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.
0
 
Ray PaseurCommented:
You can test this script here:
http://www.laprbass.com/RAY_temp_dennie.php

HTH, ~Ray
<?php // RAY_temp_dennie.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

// SET A FUTURE DATE - PUT ANYTHING YOU WANT HERE
$future = 'April 4, 2011 8:45 AM';

// MAKE A FUTURE TIMESTAMP
$future_timestamp = strtotime($future);
if (!$future_timestamp) die("BOGUS DATETIME STRING: $future");

// SET THE CURRENT TIMESTAMP
$current_timestamp = time();
$lapsed_seconds = $future_timestamp - $current_timestamp;
if ($lapsed_seconds < 1) die("IT IS TOO LATE FOR $future");

// IMPORTANT CONSTANT VALUES
$seconds_per_day    = 60*60*24;
$seconds_per_hour   = 60*60;
$seconds_per_minute = 60;

// COMPUTE DAYS AND REMOVE THEM FROM THE TOTAL SECONDS
$lapsed_days = floor($lapsed_seconds / $seconds_per_day);
$lapsed_seconds = $lapsed_seconds - ($lapsed_days * $seconds_per_day);

// COMPUTE HOURS AND REMOVE THEM FROM THE TOTAL SECONDS
$lapsed_hours = floor($lapsed_seconds / $seconds_per_hour);
$lapsed_seconds = $lapsed_seconds - ($lapsed_hours * $seconds_per_hour);

// COMPUTE MINUTES AND REMOVE THEM FROM THE TOTAL SECONDS
$lapsed_minutes = floor($lapsed_seconds / $seconds_per_minute);
$lapsed_seconds = $lapsed_seconds - ($lapsed_minutes * $seconds_per_minute);

// SHOW THE RESULT
echo PHP_EOL . "<br/>FUTURE DATETIME: $future";
echo PHP_EOL . "<br/>$lapsed_days DAYS, $lapsed_hours HOURS, $lapsed_minutes MINUTES, $lapsed_seconds SECONDS";

Open in new window

0
 
Ray PaseurCommented:
Thanks for the points - it's an interesting question.  As a result of working on it, I updated the article here:
http://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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now