Create a date and time combo

This function will enable you to create a set of combos that show the day, month, year, and minutes. You can configure what items to show and if you pass it a UNIX timestamp you will be able to select a default time.

You can use as many combos as you want in a single page as long as you give them a unique prefix.

<?php
define ('COMBO_YEAR', bindec('1'), true);
define ('COMBO_MONTH', bindec('10'), true);
define ('COMBO_DAY', bindec('100'), true);
define ('COMBO_HOUR', bindec('1000'), true);
define ('COMBO_MINUTE', bindec('10000'), true);

function CreateDateTimeCombo ($timestamp = '', $prefix = '', $options = '') {
    // Create a full combo set to enter year, month, day, hours, and minutes
    // If $timestamp is not set, then actual time is assumed
    // $options can be COMBO_YEAR | COMBO_MONTH | COMBO_DAY | COMBO_MINUTE | COMBO_HOUR
    if ($timestamp == '') {
        $timestamp = time();
    }

    if ($options == '') {
        $options = COMBO_YEAR | COMBO_MONTH | COMBO_DAY | COMBO_HOUR | COMBO_MINUTE;
    }

    $showYear = (decbin($options & COMBO_YEAR) == decbin(COMBO_YEAR));
    $showMonth = (decbin($options & COMBO_MONTH) == decbin(COMBO_MONTH));
    $showDay = (decbin($options & COMBO_DAY) == decbin(COMBO_DAY));
    $showHour = (decbin($options & COMBO_HOUR) == decbin(COMBO_HOUR));
    $showMinute = (decbin($options & COMBO_MINUTE) == decbin(COMBO_MINUTE));

    if ($showDay) {
        /* Day ========== */

        echo '<select name="' . $prefix . 'day">' . "\n";

        for ($i = 1; $i < = 31; $i++) {
            echo "\t<option ";
            if (date('d', $timestamp) == $i)
            echo "selected ";
            echo "value='".$i."'>".$i."\n";
        }
        echo '' . "\n";
    }

    if ($showMonth) {

        /* Month ========== */

        echo ' <select name="' . $prefix . 'month">' . "\n";

        for ($i = 1; $i < = 12; $i++) {
            echo "\t<option ";
            if (date('m', $timestamp) == $i)
            echo "selected ";

            $monthName = date('F', mktime(0, 0, 0, $i, 1, 2000));

            echo "value='".$i."'>{$monthName}\n";
        }
        echo '</select>' . "\n";
    }

    if ($showYear) {
        /* Year ========== */

        echo ' <select name="' . $prefix . 'year">' . "\n";

        // Start combo 5 years from actual date
        $startYear = date ('Y', mktime());

        // Actual date in db
        $actualYear = date('Y', $timestamp);

        // Stop combo 5 years after current date
        $currentYear = date ('Y', mktime());
        $stopYear = $currentYear + 1;

        for ($i = $startYear; $i < = $stopYear; $i++) {
            echo "\t<option ";
            if ($actualYear == $i)
            echo "selected ";
            echo "value='".$i."'>".$i."\n";
        }
        echo '</select>' . "\n";
    }

    if ($showHour) {
        /* Hour ========== */
        echo '     ';

        echo '<select name="' . $prefix . 'hour">' . "\n";

        for ($i = 0; $i < = 23; $i++) {
            echo "\t<option ";
            if (date('G', $timestamp) == $i)
            echo "selected ";
            printf('value="%02d">%02d', $i, $i);
        }
        echo '</select>' . "\n";
    }

    if ($showMinute) {
        /* Minute ========== */
        echo ': <select name="' . $prefix . 'minute">' . "\n";

        for ($i = 0; $i < = 59; $i++) {
            echo "\t<option ";
            if (date('i', $timestamp) == $i)
            echo "selected ";
            printf('value="%02d">%02d', $i, $i);
        }
        echo '</select>' . "\nHs";

    }
}
?>

Usage

CreateDateTimeCombo();

Now, you obviously want to parse the result of the combo back to a UNIX timestamp to be able to use it in the rest of your application. Once you submitted the form that holds the combo you need to use the following function:

<?php
function ConsolidateDatesFromCombo ($prefix) {
    // Consolidate the dates from the DateTime Combos

    $optionsArray = array('year', 'month', 'day', 'hour', 'minute');

    foreach ($optionsArray as $option) {
        if (isset($_REQUEST[$prefix . $option])) {
            $$option = doubleval($_REQUEST[$prefix . $option]);
        } else {
            $$option = 0;
        }
    }

    return mktime($hour, $minute, 0, $month, $day, $year);

}
?>

Use the same prefix in both functions to identify the same combo. For example, if you called CreateDateTimeCombo() with a prefix like 'myDate', then you need to call ConsolidateDatesFromCombo() with the same prefix.

Here's a full working example with multiple combos:

<?php

define ('COMBO_YEAR', bindec('1'), true);
define ('COMBO_MONTH', bindec('10'), true);
define ('COMBO_DAY', bindec('100'), true);
define ('COMBO_HOUR', bindec('1000'), true);
define ('COMBO_MINUTE', bindec('10000'), true);

function CreateDateTimeCombo ($timestamp = '', $prefix = '', $options = '') {
	// Create a full combo set to enter year, month, day, hours, and minutes
	// If $timestamp is not set, then actual time is assumed
	// $options can be COMBO_YEAR | COMBO_MONTH | COMBO_DAY | COMBO_MINUTE | COMBO_HOUR
	if ($timestamp == '') {
		$timestamp = time();
	}

	if ($options == '') {
		$options = COMBO_YEAR | COMBO_MONTH | COMBO_DAY | COMBO_HOUR | COMBO_MINUTE;
	}

	$showYear = (decbin($options & COMBO_YEAR) == decbin(COMBO_YEAR));
	$showMonth = (decbin($options & COMBO_MONTH) == decbin(COMBO_MONTH));
	$showDay = (decbin($options & COMBO_DAY) == decbin(COMBO_DAY));
	$showHour = (decbin($options & COMBO_HOUR) == decbin(COMBO_HOUR));
	$showMinute = (decbin($options & COMBO_MINUTE) == decbin(COMBO_MINUTE));

	if ($showDay) {
		/* Day ========== */

		echo '<select name="' . $prefix . 'day">' . "\n";

		for ($i = 1; $i < = 31; $i++) {
			echo "\t<option ";
			if (date('d', $timestamp) == $i)
			echo "selected ";
			echo "value='".$i."'>".$i."\n";
		}
		echo '' . "\n";
	}

	if ($showMonth) {

		/* Month ========== */

		echo ' <select name="' . $prefix . 'month">' . "\n";

		for ($i = 1; $i < = 12; $i++) {
			echo "\t<option ";
			if (date('m', $timestamp) == $i)
				echo "selected ";

			$monthName = date('F', mktime(0, 0, 0, $i, 1, 2000));

			echo "value='".$i."'>{$monthName}\n";
		}
		echo '</select>' . "\n";
	}


	if ($showYear) {
		/* Year ========== */

		echo ' <select name="' . $prefix . 'year">' . "\n";

		// Start combo 5 years from actual date
		$startYear = date ('Y', mktime());

		// Actual date in db
		$actualYear = date('Y', $timestamp);

		// Stop combo 5 years after current date
		$currentYear = date ('Y', mktime());
		$stopYear = $currentYear + 1;

		for ($i = $startYear; $i < = $stopYear; $i++) {
			echo "\t<option ";
			if ($actualYear == $i)
			echo "selected ";
			echo "value='".$i."'>".$i."\n";
		}
		echo '</select>' . "\n";
	}


	if ($showHour) {
		/* Hour ========== */
		echo '     ';

		echo '<select name="' . $prefix . 'hour">' . "\n";

		for ($i = 0; $i < = 23; $i++) {
			echo "\t<option ";
			if (date('G', $timestamp) == $i)
			echo "selected ";
			printf('value="%02d">%02d', $i, $i);
		}
		echo '</select>' . "\n";
	}

	if ($showMinute) {
		/* Minute ========== */
		echo "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
		echo ': <select name="' . $prefix . 'minute">' . "\n";

		for ($i = 0; $i < = 59; $i++) {
			echo "\t<option ";
			if (date('i', $timestamp) == $i)
			echo "selected ";
			printf('value="%02d">%02d', $i, $i);
		}
		echo '</select>' . "\nHs";
		echo "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

	}
}

function ConsolidateDatesFromCombo ($prefix) {
    // Consolidate the dates from the DateTime Combos

    $optionsArray = array('year', 'month', 'day', 'hour', 'minute');

    foreach ($optionsArray as $option) {
    	if (isset($_REQUEST[$prefix . $option])) {
    	    $$option = doubleval($_REQUEST[$prefix . $option]);
    	} else {
    	    $$option = 0;
    	}
    }

	return mktime($hour, $minute, 0, $month, $day, $year);
}

?>

<html>
    <head>
    <style>
    li {
    padding: 5px;
    }
    </style>
    </head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<ul>
    <li>Tomorrow: < ?php CreateDateTimeCombo(strtotime('tomorrow'), 'first_') ?></li>
    <li>Next Friday: < ?php CreateDateTimeCombo(strtotime('next Friday'), 'second_', COMBO_DAY | COMBO_MONTH | COMBO_YEAR) ?></li>
    <li>Today: < ?php CreateDateTimeCombo('', 'third_') ?></li>
</ul>

<input type="submit" value=" Send date " name="checkDate"/>
</form>

<?php
if (isset($_REQUEST['checkDate'])) {
    $first  = ConsolidateDatesFromCombo('first_');
    $second = ConsolidateDatesFromCombo('second_');
    $third  = ConsolidateDatesFromCombo('third_');

    $daqteFormat = 'l dS \of F Y h:i:s A';

    echo date($daqteFormat, $first)  . "<br>\n";
    echo date($daqteFormat, $second) . "<br />\n";
    echo date($daqteFormat, $third)  . "<br />\n";

}
?>
</body>
</html>
Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict