File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch06_Dates/timediff2.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch06_Dates/timediff2.php  >  Download  
File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch06_Dates/timediff2.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 1 month ago
Size: 1,111 bytes
 

Contents

Class file image Download
<?php

function calculate_time_difference($timestamp1, $timestamp2, $time_unit) {
   
// determines difference between two timestamps

   
$timestamp1 = intval($timestamp1);
   
$timestamp2 = intval($timestamp2);
    if (
$timestamp1 && $timestamp2) {
       
$time_lapse = $timestamp2 - $timestamp1;

       
$seconds_in_unit = array(
         
'second' => 1,
         
'minute' => 60,
         
'hour' => 3600,
         
'day' => 86400,
         
'week' => 604800,
        );
  
        if (
$seconds_in_unit[$time_unit]) {
            return
round($time_lapse/$seconds_in_unit[$time_unit]);
        }
    }

    return
false;
}

//Get the current time and seven days from now as an example
$timestamp_1 = time();
$timestamp_2 = strtotime('+7 days');

$units = array("second", "minute", "hour", "day", "week");
foreach (
$units as $u) {
   
$nunits = calculate_time_difference($timestamp_1, $timestamp_2, $u);

    echo
$nunits . " $u(s) have passed between " . date("m-d-Y", $timestamp_1) . ' and ' . date("m-d-Y", $timestamp_2);
    print
"\n";
}

print
abs(5 - 62);
print
"\n";


?>

For more information send a message to info at phpclasses dot org.