Converting a Date/Time String and Micro Time
Time and Date String Example
Micro Time:
This script took: 0.00379 seconds to execute
This script took: 0.00379 seconds to execute
<?php
include('includes/header.php');
date_default_timezone_set("America/Detroit");
error_reporting(E_ALL);
$self = basename($_SERVER['SCRIPT_NAME']);
$step = isset($_POST['step']) ? $_POST['step'] : 0;
// clean input from user
$string = isset($_POST['string']) ? $_POST['string'] : null;
$string = htmlentities($string);
if ($step == 0)
{
?>
<h1>Converting a Date/Time String and Micro Time</h1>
<h2>Time and Date String Example</h2>
<form action="<?php echo($self); ?>" method="POST">
<fieldset>
<legend>String:</legend>
<label for="string">Time String: </label>
<input type="text" size="36" id="string" name="string" value="">
</fieldset>
<p>
<input type="hidden" name="step" value="1"> <br>
<input type="submit" name="submit" value="Submit">
</p>
</form>
<?php
}
else
{
$seconds = strtotime($string); // change string into seconds
$date = getdate($seconds); // change seconds into a date
$computedDate = $date['mday'] . ' ' . $date['month'] . ', ' . $date['year'];
echo('<pre>');
print_r($date);
echo('</pre>');
$str = '';
$sec = 0;
if ($seconds < 0)
{
$sec = abs($seconds);
$str = "Seconds from $computedDate to January 1, 1970 = $sec seconds.";
}
else
{
$str = "Seconds from January 1, 1970 to $computedDate = $seconds seconds.";
}
?>
<h1>Time and Date String Part #2</h1>
<p>
String Provided: <?php echo($string); ?>
</p>
<p>
Computed Date: <?php echo($computedDate); ?>
</p>
<p>
It was a <?php echo($date['weekday']); ?>.
</p>
<p>
The year was <?php echo($date['year']); ?>.
</p>
<p>
The month was <?php echo($date['month']); ?>.
</p>
<p>
The month had <?php echo(idate('t', $seconds)); ?> days.
</p>
<p>
<?php echo($str); ?>
</p>
<p>
Current Date = <?php echo(date("d F, Y : l")); ?>
</p>
<p>
Current Time = <?php echo(time()); ?> seconds since January 1, 1970.
</p>
<form action="<?php echo($self); ?>" method="POST">
<p>
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</p>
</form>
<?php
}
?>
<?php
echo("<h2>Micro Time:</h2>");
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
for ($i=1; $i < 1000000; $i++)
{
$a = $i;
}
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime,5);
echo("<p>This script took: $totaltime seconds to execute</p>");
include('includes/footer.php');
?>