PHP Classes

PHP Time Dilation: Calculate the time dilation between objects

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 198 All time: 8,498 This week: 555Up
Version License PHP version Categories
time-dilation 1.0Public Domain5PHP 5, Physics
Description 

Author

This class can calculate the time dilation between objects.

It applies Einstein's theory of special relativity to calculate the time dilation between a moving object and a stationary one.

The class can return results from either observer frame, moving or stationary.

It can convert time and velocity elements into units per second, supporting both metric and USA standards.

Innovation Award
PHP Programming Innovation award nominee
June 2015
Number 8
According to Einstein Theory of Relativity, time dilation is a difference of elapsed time between two subjects measured by observers either moving at different speeds relative to each other.

This class can compute the difference of perceived time elapsed between two objects moving at different speeds.

Manuel Lemos
Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 16 Up2 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Example

<?PHP
/*
In this example, we use $v to represent velocity and $t to represent time. Each value is converted to seconds
to be compared against the speed of light in seconds.
*/
include('time_dilation.class.php');
$tdil = new time_dilation;

if( !empty(
$_REQUEST['formSubmitted']) ){
   
   
$velocity = ( empty($_REQUEST['velocity']) ) ? 0 : $_REQUEST['velocity'];
   
$unit = $_REQUEST['unit'];
   
$timeFrame = $_REQUEST['timeFrame'];
   
$years = ( empty($_REQUEST['years']) ) ? 0 : $_REQUEST['years'];
   
$months = ( empty($_REQUEST['months']) ) ? 0 : $_REQUEST['months'];
   
$days = ( empty($_REQUEST['days']) ) ? 0 : $_REQUEST['days'];
   
$hours = ( empty($_REQUEST['hours']) ) ? 0 : $_REQUEST['hours'];
   
$minutes = ( empty($_REQUEST['minutes']) ) ? 0 : $_REQUEST['minutes'];
   
$seconds = ( empty($_REQUEST['seconds']) ) ? 0 : $_REQUEST['seconds'];
   
$percLight = ( empty($_REQUEST['percLight']) ) ? 0 : $_REQUEST['percLight'];
   
    if(
$unit == 'metric' ){
       
$tdil->setSpeedOfLight('metric');
    }elseif(
$unit == 'usa' ){
       
$tdil->setSpeedOfLight('usa');
    }
   
    switch(
$_REQUEST['timeSelect'] ){
        case
'veryNearLight':
           
$v = $tdil->speedOfLight*.999999999;
            break;
        case
'percLight':
           
$v = $tdil->speedOfLight*($percLight/100);
            break;
        default:
           
$v = $tdil->velocityToSec($velocity,$timeFrame);
    }
   
   
$t = $tdil->timeToSec($seconds,$minutes,$hours,$days,$months,$years);
   
    switch(
$_REQUEST['observer'] ){
        case
'stationary':
           
$astro = $tdil->secToTime($t);
           
$earth = $tdil->dilation($t,$v,'stationary');
            break;
        case
'moving':
           
$earth = $tdil->secToTime($t);
           
$astro = $tdil->dilation($t,$v);
            break;
    }
}else{
   
$_REQUEST['timeSelect'] = 'percLight';
   
$_REQUEST['observer'] = 'moving';
}
?>
<html>
    <head>
        <title>Time Dilation Example</title>
        <style>
            a {
                color: black;
                text-decoration: none;
            }
            a:hover {
                color: red;
            }
        </style>
    </head>
    <body>
        <p>This example uses the classic scenario where two twins are in the space program. One is an astronaut and takes off from earth in a spaceship while the other stays on earth to monitor his progress. The faster the astronaut travels, the larger the spread will be between their ages.</p>
        <form method="post">
            <h4>Velocity</h4>
            <div>
                Astronaut will be traveling...
                <div style="margin-top:10px;">
                    <input type="radio" name="timeSelect" value="percLight"<?PHP echo ( $_REQUEST['timeSelect'] == 'percLight' ) ? ' checked' : '';?>> at <input type="text" name="percLight" value="<?PHP echo ( !empty($_REQUEST['percLight']) ) ? $_REQUEST['percLight'] : '';?>" size="4"> precent of light speed
                </div>
                <div style="margin-top:10px;">
                    <input type="radio" name="timeSelect" value="veryNearLight"<?PHP echo ( $_REQUEST['timeSelect'] == 'veryNearLight' ) ? ' checked' : '';?>> very near light speed
                </div>
                <div style="margin-top:10px;">
                    <input type="radio" name="timeSelect" value="custom"<?PHP echo ( $_REQUEST['timeSelect'] == 'custom' ) ? ' checked' : '';?>> at a rate of <input type="text" name="velocity" value="<?PHP echo ( !empty($_REQUEST['velocity']) ) ? $_REQUEST['velocity'] : '';?>" size="6">&nbsp;
                    <select name="unit">
                        <option value="usa"<?PHP echo ( !empty($_REQUEST['unit']) AND $_REQUEST['unit'] == 'usa' ) ? ' selected' : '';?>>miles</option>
                        <option value="metric"<?PHP echo ( !empty($_REQUEST['unit']) AND $_REQUEST['unit'] == 'metric' ) ? ' selected' : '';?>>kilometres</option>
                    </select>
                    <select name="timeFrame">
                        <option value="second"<?PHP echo ( !empty($_REQUEST['timeFrame']) AND $_REQUEST['timeFrame'] == 'second' ) ? ' selected' : '';?>>per Second</option>
                        <option value="minute"<?PHP echo ( !empty($_REQUEST['timeFrame']) AND $_REQUEST['timeFrame'] == 'minute' ) ? ' selected' : '';?>>per Minute</option>
                        <option value="hour"<?PHP echo ( !empty($_REQUEST['timeFrame']) AND $_REQUEST['timeFrame'] == 'hour' ) ? ' selected' : '';?>>per Hour</option>
                    </select>
                <div>
            </div>
            <h4>Time</h4>
            <div>
                Astronaut will be traveling for...<br>
                <input type="text" name="years" size="4" value="<?PHP echo ( !empty($_REQUEST['years']) ) ? $_REQUEST['years'] : '';?>"> Year(s)<br>
                <input type="text" name="months" size="4" value="<?PHP echo ( !empty($_REQUEST['months']) ) ? $_REQUEST['months'] : '';?>"> Month(s)<br>
                <input type="text" name="days" size="4" value="<?PHP echo ( !empty($_REQUEST['days']) ) ? $_REQUEST['days'] : '';?>"> Day(s)<br>
                <input type="text" name="hours" size="4" value="<?PHP echo ( !empty($_REQUEST['hours']) ) ? $_REQUEST['hours'] : '';?>"> Hour(s)<br>
                <input type="text" name="minutes" size="4" value="<?PHP echo ( !empty($_REQUEST['minutes']) ) ? $_REQUEST['minutes'] : '';?>"> Minute(s)<br>
                <input type="text" name="seconds" size="4" value="<?PHP echo ( !empty($_REQUEST['seconds']) ) ? $_REQUEST['seconds'] : '';?>"> Second(s)<br>
            </div>
            <h4>Observer</h4>
            <div>
                As observed by the...<br>
                <input type="radio" name="observer" value="moving"<?PHP echo ( $_REQUEST['observer'] == 'moving' ) ? ' checked' : '';?>> Astronaut<br>
                <input type="radio" name="observer" value="stationary"<?PHP echo ( $_REQUEST['observer'] == 'stationary' ) ? ' checked' : '';?>> Earth<br>
            </div>
            <div style="margin-top:20px;">
                <input type="hidden" name="formSubmitted" value="1">
                <input type="submit" name="submitForm" value="Calculate">&nbsp;
                [<a href="example.php">start over</a>]
            </div>
        </form>
<?PHP
if( !empty($astro) ){
?>
<div style="margin-top:20px;">
            Astronaut traveled for <?PHP echo $earth['years'];?> years, <?PHP echo $earth['months'];?> months, <?PHP echo $earth['days'];?> days, <?PHP echo $earth['hours'];?> hours, <?PHP echo $earth['minutes'];?> minutes, <?PHP echo $earth['seconds'];?> seconds and <?PHP echo $earth['remainder'];?> of a second
        </div>
        <div style="margin-top:10px;">
            The time passed on earth is <?PHP echo $astro['years'];?> years, <?PHP echo $astro['months'];?> months, <?PHP echo $astro['days'];?> days, <?PHP echo $astro['hours'];?> hours, <?PHP echo $astro['minutes'];?> minutes, <?PHP echo $astro['seconds'];?> seconds and <?PHP echo $astro['remainder'];?> of a second
        </div>
<?PHP
}
?>
</body>
</html>


  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example Usage
Plain text file time_dilation.class.php Class Main Class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:198
This week:0
All time:8,498
This week:555Up