Jump to content

Modify script?


Genesis730

Recommended Posts

I have this script that takes a UNIX timestamp and determines how long it will be or has been since the timestamp... I was hoping somebody could help me modify it to also show the current array plus the one before it... so if there's 1 week, it'll show x days or if theres 1 month it'll show x weeks, etc... to get a more accurate reading.

Here is the script...

 

<?php
    function timeSince($timestamp) {
        $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1, 'second')
    );

    $since = time() - $timestamp;

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {    
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        if(($count = floor($since / $seconds)) != 0) break;
    }

    $printtime = ($count == 1) ? "1 {$name}" : "$count {$name}s";

    return $printtime . " ago";
    }

    function timeUntil($timestamp) {
        $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1, 'second')
    );

    $until = $timestamp - time();

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {    
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        if(($count = floor($until / $seconds)) != 0) break;
    }
    
    if($count < 0) return "Expired ".timeSince($original);

    $printtime = ($count == 1) ? "1 {$name}" : "$count {$name}s";

    return $printtime;
}

?>

Link to comment
Share on other sites

  • 1 month later...
<?php
//implement leap year function
function isleapyear($year = '') {
   if (empty($year)) {
      $year = date('Y');
   }
   $year = (int) $year;
   if ($year % 4 == 0) {
      if ($year % 100 == 0) {
         return ($year % 400 == 0);
      } else {
         return true;
      }
   } else {
      return false;
   }
}

function date_diff($date, $date2 = 0)
{
    if(!$date2)
        $date2 = mktime();

    $date_diff = array('seconds'  => '',
                       'minutes'  => '',
                       'hours'    => '',
                       'days'     => '',
                       'weeks'    => '',
                       'years'    => '',
                       
                       'tseconds' => '',
                       'tminutes' => '',
                       'thours'   => '',
                       'tdays'    => '',
                       'tweeks'   => '',
                       'tyears'    => '');
  
    if($date2 > $date)
        $tmp = $date2 - $date;
    else
        $tmp = $date - $date2;

    $seconds = $tmp;
    
    $date_diff['years'] = floor($tmp/31556926);
    $tmp -= $date_diff['years'] * 31556926;

    $date_diff['weeks'] = floor($tmp/604800);
    $tmp -= $date_diff['weeks'] * 604800;

    $date_diff['days'] = floor($tmp/86400);
    $tmp -= $date_diff['days'] * 86400;

    $date_diff['hours'] = floor($tmp/3600);
    $tmp -= $date_diff['hours'] * 3600;

    $date_diff['minutes'] = floor($tmp/60);
    $tmp -= $date_diff['minutes'] * 60;

    $date_diff['seconds'] = $tmp;
    
    $date_diff['tyears'] = floor($seconds/31556926);
    $date_diff['tweeks'] = floor($seconds/604800);
    $date_diff['tdays'] = floor($seconds/86400);
    $date_diff['thours'] = floor($seconds/3600);
    $date_diff['tminutes'] = floor($seconds/60);
    $date_diff['tseconds'] = $seconds;

    return $date_diff;
}

$time1_value = "now";//change this
$time2_value = "next Saturday";//change this
$time1 = strtotime($time1_value);
$time2 = strtotime($time2_value);
$diff = date_diff($time1, $time2);

echo "Time between <b>$time1_value</b> and <b>$time2_value</b><br />";
echo "Years: " .$diff['years']. "<br />";
echo "Weeks: " .$diff['weeks']. "<br />";
echo "Days: " .$diff['days']. "<br />";
echo "Hours: " .$diff['hours']. "<br />";
echo "Minutes: " .$diff['minutes']. "<br />";
echo "Seconds: " .$diff['seconds']. "<br />";

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.