Veronica's "String To Time" Form

String:


Enter a string and click submit to see how that string is evaluated through the strtotime() function. Other details are added, like how many seconds, minutes, hours, and days are from now to your string. See how long until your birthday or another important date.

Try entering strings like:
"Tomorrow 1:30pm"
"December 31, 2024"
"Yesterday"
"Next Month"
"+2 weeks Monday"
"-3 Saturdays"

 

CODE FOLLOWS

<?php

include('includes/header.php');

// set timezones
date_default_timezone_set("America/Detroit");

// set error reporting to all
error_reporting(E_ALL);

// variables
$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>Veronica's "String To Time" Form</h1>

    <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>

    <p>
        Enter a string and click submit to see how that string is evaluated
        through the strtotime() function. Other details are added, like how
        many seconds, minutes, hours, and days are from now to your string.
        See how long until your birthday or another important date.
    </p>

    <p>
        Try entering strings like:<br>"Tomorrow 1:30pm"<br>"December 31, 2024"<br>"Yesterday"
        <br>"Next Month"<br>"+2 weeks Monday"<br>"-3 Saturdays"
    </p>

    <?php
}
else
{
    
// change string to seconds
    
$seconds strtotime($string);
    
// get current date, time
    
$today date("Y-m-d H:i:s");
    
$time time();
    
// get the difference between string and now
    
$sec_difference $seconds $time;
    
$min_difference floor($sec_difference 60);
    
$hour_difference floor($min_difference 60);
    
$day_difference floor($hour_difference 24);
    
?>

    <pre>
            <?php
            
echo("It is Now: $today");
            
?>
        </pre>

    <p>
        String Given: <?php echo($string); ?>
    </p>

    <p>
        Seconds computed from String: <?php echo($seconds); ?>
    </p>

    <p>
        Current Time (seconds): <?php echo($time); ?>
    </p>

    <p>
        Difference between Time (seconds) and Seconds computed from String: <?php echo($sec_difference); ?>
        <br>Difference in minutes: <?php echo($min_difference); ?>
        <br>Difference in hours: <?php echo($hour_difference); ?>
        <br>Difference in days: <?php echo($day_difference); ?>
    </p>

    <p>
        Current Date, Reformatted: <?php echo(date("l, d F, Y")); ?>
    </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
}
// end of else statement

include('includes/footer.php');

?>