i want to show calendar in php show in image below connected to oracle database table. ywub5.png

Dani AI

Generated

wanted a month-style PHP calendar backed by Oracle and a navigation bar showing previous/next months; was right that more detail was needed. Below is a compact, practical pattern that covers the date-navigation logic, a safe Oracle query to fetch only the visible month, and how to group events by day so the calendar grid can render them.

Core idea: pick the month from a query string (format YYYY-MM) or default to the current month. Compute an inclusive start date (first day) and an exclusive end date (first day of next month). Query Oracle with binds and explicit formats so NLS settings do not change results. Group results by day number and render a simple nav using ?ym=YYYY-MM links.

<?php
// determine month to show (YYYY-MM)
$ym = isset($_GET['ym']) ? $_GET['ym'] : date('Y-m');
try { $dt = new DateTime($ym . '-01'); } catch (Exception $e) { $dt = new DateTime('first day of this month'); }

$start = $dt->format('Y-m-d');                     // inclusive
$end   = (clone $dt)->modify('+1 month')->format('Y-m-d'); // exclusive

$prevYm = (clone $dt)->modify('-1 month')->format('Y-m');
$nextYm = (clone $dt)->modify('+1 month')->format('Y-m');

$conn = oci_connect($user, $pass, $db); // set credentials
$sql = "SELECT id, title, TO_CHAR(TRUNC(event_date),'YYYY-MM-DD') evdate
        FROM events
        WHERE event_date >= TO_DATE(:start,'YYYY-MM-DD')
          AND event_date <  TO_DATE(:end,'YYYY-MM-DD')";

$stid = oci_parse($conn, $sql);
oci_bind_by_name($stid, ':start', $start);
oci_bind_by_name($stid, ':end', $end);
oci_execute($stid);

$eventsByDay = [];
while ($r = oci_fetch_assoc($stid)) {
    $day = (int)substr($r['EVDATE'], 8, 2);
    $eventsByDay[$day][] = $r;
}
?>
<!-- simple nav -->
<a href="?ym=<?php echo $prevYm; ?>">&lt;&lt; <?php echo (clone $dt)->modify('-1 month')->format('M Y'); ?></a>
<span><?php echo $dt->format('F Y'); ?></span>
<a href="?ym=<?php echo $nextYm; ?>"><?php echo (clone $dt)->modify('+1 month')->format('M Y'); ?> &gt;&gt;</a>

Notes and troubleshooting

  • Use parameterized binds (shown) to avoid injection and to let Oracle do date conversion reliably.
  • Prefer TO_CHAR(TRUNC(event_date),'YYYY-MM-DD') so PHP receives a stable date string regardless of NLS_DATE_FORMAT.
  • If events store times, TRUNC(event_date) ensures events fall on the correct day.
  • Ensure PHP has the OCI8 (or PDO_OCI) extension enabled. Test with phpinfo().
  • Index the event_date column and fetch only required columns for performance.
  • If timezones matter, normalize DB and PHP times before comparing.

This pattern covers the next/previous bar labels, safe Oracle querying for one month, and grouping events for a month grid.

Recommended Answers

All 2 Replies

i want to display date in navigation bar(next and previous) using oracle database in php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.