<?php
// i_eg.php
// RJM Programming - November, 2024
// Thanks to https://www.php.net/manual/en/intldateformatter.format.php and https://www.php.net/manual/en/intlcalendar.createinstance.php and https://stackoverflow.com/questions/44693939/incorrect-hijri-date-in-php-intl

echo "<html><head><meta charset='UTF-8'><title>PHP Intl Current Time via Locale and TimeZone and optional Calendar - RJM Programming - December, 2024</title></head><body>";
if (isset($_GET['locale']) && isset($_GET['timezone']) && isset($_GET['calendar'])) {
if (1 == 6) {
$tz = reset(iterator_to_array(IntlTimeZone::createEnumeration(strtoupper(substr(urldecode($_GET['locale']),0,2)))));
$formatter = IntlDateFormatter::create(
    urldecode($_GET['locale']),
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    $tz,
    IntlDateFormatter::GREGORIAN
);

//ini_set('intl.default_locale', urldecode($_GET['locale']));
//ini_set('date.timezone', urldecode($_GET['timezone']));

//$cal = IntlCalendar::createInstance();
$cal = IntlCalendar::createInstance($tz, '@calendar=' . urldecode($_GET['calendar']));
$cal->set(IntlCalendar::FIELD_MONTH, 0); // 1st month, 1st day ... 9th month, Ramadan
$cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 1); //1st day
$cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$cal->clear(IntlCalendar::FIELD_MINUTE);
$cal->clear(IntlCalendar::FIELD_SECOND);
$cal->clear(IntlCalendar::FIELD_MILLISECOND);

echo "In this " . explode('-', urldecode($_GET['calendar']))[0] . " year, the first day of the year started/will start on:\n\t",
        $formatter->format($cal), "\n";

//Itʼs the formatterʼs timezone that is used:
$formatter->setTimeZone(urldecode($_GET['timezone']));
echo "After changing timezone:\n\t",
        $formatter->format($cal), "\n";
} else {
$DateTime = new DateTime();
$IntlDateFormatter = new IntlDateFormatter(
                    urldecode($_GET['locale']) . '@calendar=' . urldecode($_GET['calendar']),
                    IntlDateFormatter::FULL,
                    IntlDateFormatter::FULL,
                    urldecode($_GET['timezone']),
                    IntlDateFormatter::TRADITIONAL);

echo '<p>' . $IntlDateFormatter->format($DateTime) . '</p><br><br>';
echo "<h1>Show Current Time</h1><br><h3>RJM Programming - December, 2024</h3><br><h4>As per (white background textboxes optional) ...</h4><br><br><form method=POST onsubmit=\"if (document.getElementById('calendar').value.trim() != '') { document.getElementById('calendar').name='calendar';  } if (document.getElementById('locale').value.trim() == '' || document.getElementById('timezone').value.trim() == '') { return false; } return true;\" action=\"./i_eg.php\">
<br><input style=background-color:yellow; id=locale name=locale placeholder=en_AU value='' title=Locale></input>
<br><input style=background-color:yellow; id=timezone name=timezone placeholder=Australia/Sydney value='' title=TimeZone></input>
<br><input style=background-color:white; id=calendar placeholder=gregorian value='' title=Calendar></input>
<br><br><input style=background-color:lightgreen; type=submit value=Display></input>
</form>
";
}
} else if (isset($_GET['locale']) && isset($_GET['timezone']) && !isset($_GET['calendar'])) {
$fmt = datefmt_create(
    urldecode($_GET['locale']),
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    urldecode($_GET['timezone']),
    IntlDateFormatter::GREGORIAN
);
echo '<p>' . datefmt_format($fmt, time()) . '</p><br><br>';
echo "<h1>Show Current Time</h1><br><h3>RJM Programming - December, 2024</h3><br><h4>As per (white background textboxes optional) ...</h4><br><br><form method=POST onsubmit=\"if (document.getElementById('calendar').value.trim() != '') { document.getElementById('calendar').name='calendar';  } if (document.getElementById('locale').value.trim() == '' || document.getElementById('timezone').value.trim() == '') { return false; } return true;\" action=\"./i_eg.php\">
<br><input style=background-color:yellow; id=locale name=locale placeholder=en_AU value='' title=Locale></input>
<br><input style=background-color:yellow; id=timezone name=timezone placeholder=Australia/Sydney value='' title=TimeZone></input>
<br><input style=background-color:white; id=calendar placeholder=gregorian value='' title=Calendar></input>
<br><br><input style=background-color:lightgreen; type=submit value=Display></input>
</form>
";
} else if (isset($_POST['locale']) && isset($_POST['timezone']) && isset($_POST['calendar'])) {
$DateTime = new DateTime();
$IntlDateFormatter = new IntlDateFormatter(
                    urldecode($_POST['locale']) . '@calendar=' . urldecode($_POST['calendar']),
                    IntlDateFormatter::FULL,
                    IntlDateFormatter::FULL,
                    urldecode($_POST['timezone']),
                    IntlDateFormatter::TRADITIONAL);

echo '<p>' . $IntlDateFormatter->format($DateTime) . '</p><br><br>';
echo "<h1>Show Current Time</h1><br><h3>RJM Programming - December, 2024</h3><br><h4>As per (white background textboxes optional) ...</h4><br><br><form method=POST onsubmit=\"if (document.getElementById('calendar').value.trim() != '') { document.getElementById('calendar').name='calendar';  } if (document.getElementById('locale').value.trim() == '' || document.getElementById('timezone').value.trim() == '') { return false; } return true;\" action=\"./i_eg.php\">
<br><input style=background-color:yellow; id=locale name=locale placeholder=en_AU value='' title=Locale></input>
<br><input style=background-color:yellow; id=timezone name=timezone placeholder=Australia/Sydney value='' title=TimeZone></input>
<br><input style=background-color:white; id=calendar placeholder=gregorian value='' title=Calendar></input>
<br><br><input style=background-color:lightgreen; type=submit value=Display></input>
</form>
";
} else if (isset($_POST['locale']) && isset($_POST['timezone']) && !isset($_POST['calendar'])) {
$fmt = datefmt_create(
    urldecode($_POST['locale']),
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    urldecode($_POST['timezone']),
    IntlDateFormatter::GREGORIAN
);
echo '<p>' . datefmt_format($fmt, time()) . '</p><br><br>';
echo "<h1>Show Current Time</h1><br><h3>RJM Programming - December, 2024</h3><br><h4>As per (white background textboxes optional) ...</h4><br><br><form method=POST onsubmit=\"if (document.getElementById('calendar').value.trim() != '') { document.getElementById('calendar').name='calendar';  } if (document.getElementById('locale').value.trim() == '' || document.getElementById('timezone').value.trim() == '') { return false; } return true;\" action=\"./i_eg.php\">
<br><input style=background-color:yellow; id=locale name=locale placeholder=en_AU value='' title=Locale></input>
<br><input style=background-color:yellow; id=timezone name=timezone placeholder=Australia/Sydney value='' title=TimeZone></input>
<br><input style=background-color:white; id=calendar placeholder=gregorian value='' title=Calendar></input>
<br><br><input style=background-color:lightgreen; type=submit value=Display></input>
</form>
";
} else if (isset($_GET['test'])) {
$fmt = datefmt_create(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN
);
echo '<p>First Formatted output is ' . datefmt_format($fmt, 0) . '</p><br>';

$fmt = datefmt_create(
    'de-DE',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN
);
echo '<p>Second Formatted output is ' . datefmt_format($fmt, 0) . '</p><br>';

$fmt = datefmt_create(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN,
    'MM/dd/yyyy'
);
echo '<p>First Formatted output with pattern is ' . datefmt_format($fmt, 0) . '</p><br>';

$fmt = datefmt_create(
    'de-DE',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN,
    'MM/dd/yyyy'
);
echo "<p>Second Formatted output with pattern is " . datefmt_format($fmt, 0) . '</p><br>';
} else {
echo "<h1>Show Current Time</h1><br><h3>RJM Programming - December, 2024</h3><br><h4>As per (white background textboxes optional) ...</h4><br><br><form method=GET onsubmit=\"if (document.getElementById('calendar').value.trim() != '') { document.getElementById('calendar').name='calendar';  } if (document.getElementById('locale').value.trim() == '' || document.getElementById('timezone').value.trim() == '') { return false; } return true;\" action=\"./i_eg.php\">
<br><input style=background-color:yellow; id=locale name=locale placeholder=en_AU value='' title=Locale></input>
<br><input style=background-color:yellow; id=timezone name=timezone placeholder=Australia/Sydney value='' title=TimeZone></input>
<br><input style=background-color:white; id=calendar placeholder=gregorian value='' title=Calendar></input>
<br><br><input style=background-color:lightgreen; type=submit value=Display></input>
</form>
";
}
echo "</body></html>";
?>
