<?php
/**
 * This class acts as a formatter for the dates. It should be called on
 * every timestamp stored.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class DateFormatter {

    /**
     * Formats the given timestamp in the 'day / month / year' format.
     *
     * @param int $timestamp
     * @return string
     *
     * @deprecated Use formatDMY() or formatDMYHM() instead
     */
    public static function format($timestamp) {
        $format = "d / m / Y";
        $date = date($format, $timestamp);
        return $date;
    }

    /**
     * Formats the given timestamp in the 'day / month / year at hour:minutes'
     * format.
     *
     * It checks that the date is not at midnight. If it is the case, it
     * doesn't display the hour:minutes part.
     *
     * @param int $timestamp
     * @return string
     */
    public static function formatDMYHM($timestamp) {
        $format = "d / m / Y";
        $date = date($format, $timestamp);

        $format = "H:i";
        $hour = date($format, $timestamp);

        if ($hour == "00:00") {
            return $date;
        } else {
            return $date . " " . Localization::at . " " . $hour;
        }
    }

    /**
     * Formats the given timestamp in the 'day / month / year' format.
     *
     * @param int $timestamp
     * @return string
     */
    public static function formatDMY($timestamp) {
        $format = "d / m / Y";
        $date = date($format, $timestamp);
        return $date;
    }

    /**
     * Formats the given timestamp to a date following the RFC2822 ('r').
     *
     * @param int $timestamp
     * @return string
     */
    public static function formatRFC($timestamp) {
        $format = "r";
        return date($format, $timestamp);
    }

    public static function formatSQL($timestamp) {
        return date("Y-m-d H:i:s", $timestamp);
    }

    public static function GetDMYForm($inputName, $timestamp) {
        $days = "<select name='" . $inputName . "_days'>";
        $selectedDay = date("d", $timestamp);
        for ($index = 1; $index <= 31; $index++) {
            $selected = ($index == $selectedDay) ? "selected" : "";
            $days .= "<option value='$index' $selected>$index</option>";
        }
        $days .= "</select>";

        $months = "<select name='" . $inputName . "_months'>";
        $selectedMonth = date("m", $timestamp);
        for ($index = 1; $index <= 12; $index++) {
            $selected = ($index == $selectedMonth) ? "selected" : "";
            $months .= "<option value='$index' $selected>$index</option>";
        }
        $months .= "</select>";

        $selectedYear = date("Y", $timestamp);
        $years = "<input name='". $inputName . "_year' value ='$selectedYear'/>";

        $form = $days . " / " .  $months . " / " . $years;

        return $form;
    }

    public static function getHHMMForm($inputName, $timestamp) {
        $hours = "<select name='" . $inputName . "_hours'>";
        $selectedHour = date("H", $timestamp);
        for ($index = 0; $index <= 23; $index++) {
            $selected = ($index == $selectedHour) ? "selected" : "";
            $hours .= "<option value='$index' $selected>$index</option>";
        }
        $hours .= "</select>";

        $minutes = "<select name='" . $inputName . "_minutes'>";
        $selectedMinutes = date("i", $timestamp);
        for ($index = 0; $index <= 59; $index++) {
            $selected = ($index == $selectedMinutes) ? "selected" : "";
            $minutes .= "<option value='$index' $selected>$index</option>";
        }
        $minutes .= "</select>";

        $checked = ($selectedMinutes == 0 && $selectedHour == 0) ? "checked" : "";
        $wholeDay = "<label for='" . $inputName . "_wholeDay'>" . Localization::newsWholeDay . "</label>";
        $wholeDay .= "<input type='checkbox' name='". $inputName . "_wholeDay' id='". $inputName . "_wholeDay' $checked/>";

        $form = $hours . " : " .  $minutes . " <br> " . $wholeDay;

        return $form;
    }
}
?>
